C#从无WSDL的持续推送Web服务接收数据存SQL报错排查
Fixing "Cannot send a content-body with this verb-type" Error in C# GET Request
What's Causing the Error?
The error pops up because you’re calling request.GetRequestStream() for a GET request. HTTP GET requests don’t support sending a request body (content-body), so this method call is completely invalid here. You can safely delete that line entirely—it’s the root of your problem.
Corrected Basic Code
First, let’s fix the immediate error in your existing code:
using System; using System.IO; using System.Net; class Program { static void Main() { string url = "http://XXXXXX:7777/online?user=ir-U-butaneco&pass=ir-Ue724679821pars6843560G1P1S"; WebRequest request = WebRequest.Create(url); request.Method = "GET"; using (WebResponse response = request.GetResponse()) { Console.WriteLine("Content type is {0}", response.ContentType); Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Remove the request.GetRequestStream() line—it's not needed for GET using (Stream dataStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(dataStream)) { string responseFromServer = reader.ReadToEnd(); Console.WriteLine(responseFromServer); } } } }
Handling Continuous Data from the Server
Since you mentioned the web server continuously sends data, ReadToEnd() isn’t ideal—it’ll block until the server closes the connection. Instead, read the stream in a loop to process incoming data as it arrives:
using System; using System.IO; using System.Net; using System.Data.SqlClient; class Program { static void Main() { string url = "http://XXXXXX:7777/online?user=ir-U-butaneco&pass=ir-Ue724679821pars6843560G1P1S"; WebRequest request = WebRequest.Create(url); request.Method = "GET"; using (WebResponse response = request.GetResponse()) using (Stream dataStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(dataStream)) { Console.WriteLine("Content type is {0}", response.ContentType); Console.WriteLine(((HttpWebResponse)response).StatusDescription); string incomingData; // Read line by line as data streams in while ((incomingData = reader.ReadLine()) != null) { Console.WriteLine("Received: " + incomingData); // Store each piece of data to SQL immediately SaveToDatabase(incomingData); } } } private static void SaveToDatabase(string data) { // Replace with your actual SQL connection string and table schema string connectionString = "Server=yourServer;Database=yourDB;User Id=yourUser;Password=yourPass;"; string insertQuery = "INSERT INTO YourDataTable (ReceivedContent) VALUES (@Data)"; using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(insertQuery, conn)) { cmd.Parameters.AddWithValue("@Data", data); cmd.ExecuteNonQuery(); } } } }
Key Tips
- Always wrap
WebResponse,Stream, andStreamReaderinusingstatements to make sure resources are cleaned up properly. - For continuous streams, reading line-by-line (or in chunks) lets you process data in real time instead of waiting for the connection to end.
- Don’t hardcode your SQL connection string—use configuration files like
appsettings.jsonfor security and flexibility. - If the server sends binary data instead of text, swap out
StreamReaderfor directStreamreading logic.
内容的提问来源于stack exchange,提问作者Nader Zare
相关产品推荐
相关产品推荐

