我正在尝试从压缩器接收数据,但答案始终是空的。这是我的代码:
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#中的第一个压缩器获取数据吗?
答案 0 :(得分:1)
我比较了Fiddler 4中的三个请求,意识到vbs脚本和两个WebClient之间唯一的区别(除了一些其他的不会影响行为的标头)
而HttpWebRequest是,托管API发送Expect: 100-continue
标头,而vbs脚本不发送。
如果在压缩机设备上运行的软件不支持此功能,则可能是问题。
请尝试以下操作,告诉HttpWebRequest不要发送此标头:
对于HttpWebRequest,您可以通过以下方式阻止发送该邮件:
request.ServicePoint.Expect100Continue = false;
注意:对于WebClient,分配需要访问内部使用的HttpWebRequest对象,但这具有“受保护的”访问修饰符,可以解决。
设置此值之前:
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
答案 1 :(得分:0)
WebClient和HttpWebRequest仍然不能与此comressor一起使用。我试图添加任何标题,但没有结果。
我尝试使用PowerShell,它也返回空响应:
Invoke-WebRequest -Uri http://10.0.163.51/getVar.cgi -Method POST -Body "package_discharge_pressure" -ContentType "application/x-www-form-urlencoded"
卷曲也不起作用,它挂起没有错误
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