来自 webclient C# 的空答案

Empty answer from webclient C#

我正在尝试从压缩器接收数据,但它的答案总是空的。这是我的代码:

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# 从第一个压缩器获取数据吗?

我比较了 Fiddler 4 中的三个请求并意识到 vbs 脚本和 WebClient 之间的唯一区别(除了一些不会影响行为的其他 headers) 和 HttpWebRequest 是,托管 API 发送 Expect: 100-continue header 而 vbs 脚本没有。

如果压缩器设备上的软件 运行 不支持此功能,则可能会出现此问题。

请尝试以下操作,它告诉 HttpWebRequest 不要发送此 header:

对于 HttpWebRequest,您可以通过以下方式简单地阻止发送:

 request.ServicePoint.Expect100Continue = false;

注意:对于 WebClient,分配需要访问内部使用的 HttpWebRequest object,但这有 "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 和其他一些 header:

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 和 HttpWebRequest 仍然不适用于此压缩器。我尝试添加任何 headers,但没有结果。

我试过 PowerShell,它也 returns 空响应:

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