You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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, and StreamReader in using statements 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.json for security and flexibility.
  • If the server sends binary data instead of text, swap out StreamReader for direct Stream reading logic.

内容的提问来源于stack exchange,提问作者Nader Zare

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:03:14