C#中WebClient返回空响应

4

我正试图从压缩机接收数据,但它的响应始终为空。以下是我的代码:

WebClient client = new WebClient();
client.Headers[HttpRequestHeader.ContentType]= "application/x-www-form-urlencoded";
string question = "online_pressure"; 
string URL = "http://10.0.163.51/getVar.cgi"; 
string answer = client.UploadString(URL, "POST", question);
Console.WriteLine(answer);

当我将此代码用于另一个压缩器时,只有2个字符串与原先不同,它可以正常工作,并且我能在控制台中看到答案:
string question = "QUESTION=300201"; 
string URL = "http://10.0.163.50/cgi-bin/mkv.cgi";

VBS代码对于两个压缩机都能很好地运行。我可以从第一个和第二个压缩机的MsgBox中看到结果:

Dim objHTTP
strToSend = "online_pressure"
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
Call objHTTP.Open("POST", "http://10.0.163.51/getVar.cgi", false)
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.Send strToSend
MsgBox(objHTTP.ResponseText)

HttpRequest代码也适用于第二个压缩机:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;
    StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
    requestWriter.Write(data);
    requestWriter.Close();

    try
    {
        // get the response
        WebResponse webResponse = request.GetResponse();
        Stream webStream = webResponse.GetResponseStream();
        StreamReader responseReader = new StreamReader(webStream);
        string response = responseReader.ReadToEnd();
        responseReader.Close();
        Console.WriteLine(response);
    }
    catch (WebException we)
    {
        string webExceptionMessage = we.Message;
    }

我应该尝试哪些方法来获取C#中第一个压缩机的数据?

2个回答

1
WebClient和HttpWebRequest仍无法与此压缩器一起使用。我尝试添加任何标头,但没有结果。
我尝试了PowerShell,它也返回空响应:
Invoke-WebRequest -Uri http://10.0.163.51/getVar.cgi -Method POST -Body "package_discharge_pressure" -ContentType "application/x-www-form-urlencoded"

Curl也无法工作,它会无故挂起而没有错误提示。
curl -i -X POST -H "Content-Type:application/x-www-form-urlencoded" -d "sump_pressure" http://10.0.163.51/getVar.cgi

Javascript和VBS运行良好。今天我找到了一段也能工作的C#代码。

WinHttpRequest req = new WinHttpRequest();
try {
    req.Open("POST", "http://10.0.163.51/getVar.cgi", true);
    req.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.Send("sump_pressure");
    req.WaitForResponse();
}
catch(Exception ex) {
    Console.WriteLine("Error : " + ex.Message.Trim());
}
Console.WriteLine(req.ResponseText);

在Visual Studio中需要引用WinHttp


1
我在 Fiddler 4 中比较了这三个请求,发现除了一些其他不影响行为的头文件之外,vbs 脚本和 WebClient 和 HttpWebRequest 之间唯一的区别是托管 API 发送了 Expect: 100-continue 头文件,而 vbs 脚本没有发送。
如果压缩设备上运行的软件不支持此功能,则可能会出现问题。
请尝试以下操作,告诉 HttpWebRequest 不要发送此头文件:
对于 HttpWebRequest,您可以通过以下方法简单地防止发送它:
 request.ServicePoint.Expect100Continue = false;

注意:对于WebClient,这个任务需要访问内部使用的HttpWebRequest对象,但它有"protected"访问修饰符,可以通过解决方案来处理。
在设置此值之前:
POST http://oguzozgul.com.tr/getVar.cgi HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: oguzozgul.com.tr
Content-Length: 15
Expect: 100-continue
Connection: Keep-Alive

online_pressure 

在设置了这个值之后:
POST http://oguzozgul.com.tr/getVar.cgi HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: oguzozgul.com.tr
Content-Length: 15
Connection: Keep-Alive

online_pressure

我还想在这里放置VB脚本请求,以便您也可以看到其他差异。默认情况下也不发送Accept-Encoding和一些其他标头:

POST http://oguzozgul.com.tr/getVar.cgi HTTP/1.1
Accept: */*
Content-Type: application/x-www-form-urlencoded
Accept-Language: tr,en-US;q=0.8,en-GB;q=0.7,en;q=0.5,zh-Hans-CN;q=0.3,zh-Hans;q=0.2
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Win64; x64; Trident/7.0; .NET4.0E; .NET4.0C; Tablet PC 2.0; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; wbx 1.0.0; Zoom 3.6.0; wbxapp 1.0.0)
Host: oguzozgul.com.tr
Content-Length: 15
Connection: Keep-Alive
Pragma: no-cache

online_pressure

谢谢您的回答!我在WebClient之后添加了这段代码,也在之前添加了,但仍然不起作用...而且我不理解“您应该获取现有值的备份,查询压缩器并将其设置回来。” 您是指在查询插入ServicePointManager.Expect100Continue = true;之后吗? - Alexey Kin
很抱歉听到这个消息。是的,如果它之前为真,将其设置回真是我所指的。 - Oguz Ozgul
我认为你应该比较发送的标头。问题一定在那里,可能是用户代理的问题。它不是默认发送的(虽然不应该是问题,但可以尝试一下)。 - Oguz Ozgul
1
[HttpWebRequest].ServicePoint.Expect100Continue = false;设置到当前请求的ServicePoint而不是ServicePointManager的通用设置中。添加一个User-Agent头和[HttpWebRequest].Headers.Add(HttpRequestHeader.CacheControl, "no-cache");。如果失败,查看HttpStatusCode。 - Jimi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接