gpt4 book ai didi

c# - 来自 webclient C# 的空答案

转载 作者:行者123 更新时间:2023-12-03 19:14:31 24 4
gpt4 key购买 nike

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

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 之间的唯一区别(除了一些不会影响行为的其他 header )
和 HttpWebRequest 是,托管 API 发送 Expect: 100-continue header 和 vbs 脚本没有。

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

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

对于 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 和其他一些 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

关于c# - 来自 webclient C# 的空答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61218038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com