如何:在c#NET中使用httpget
有人能帮帮我吗。我尝试过各种.NET的东西: WebClient、HttpWebResponse、WebResponse等。。。来自网络的各种建议 我正在尝试从设备读取值。我就是这样做的:如何:在c#NET中使用httpget,c#,http,post,C#,Http,Post,有人能帮帮我吗。我尝试过各种.NET的东西: WebClient、HttpWebResponse、WebResponse等。。。来自网络的各种建议 我正在尝试从设备读取值。我就是这样做的: C:\Users\Alexm>httpget -d -r -S "*SRTC\r" http://isd-9138:2000 Host: isd-9138:2000, URL Host: isd-9138:2000, Name: / GET / HTTP/1.0 User-Agent: Mozilla/
C:\Users\Alexm>httpget -d -r -S "*SRTC\r" http://isd-9138:2000
Host: isd-9138:2000, URL Host: isd-9138:2000, Name: /
GET / HTTP/1.0
User-Agent: Mozilla/2.0 (Win95; I)
Pragma: no-cache
Host: isd-9138:2000
Accept: */*
POSTING 6 bytes...
0027.3
C:\Users\Alexm>
我如何在c#中简单地实现这一点
谢谢
供参考:
例如,这里有一个尝试:
try
{
string url = "http://" + IPs[0] + ":" + _DriverConfig.SensorPort.ToString();
string cmd = "*SRTC\r";
// url: http://10.10.11.105:2000
using (WebClient client = new WebClient())
{
var response = client.UploadString(url, cmd);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
例:信息:
"The server committed a protocol violation. Section=ResponseStatusLine"
"The server committed a protocol violation. Section=ResponseStatusLine"
堆栈跟踪:
" at System.Net.WebClient.UploadDataInternal(Uri address, String method,
Byte[] data, WebRequest& request)
at System.Net.WebClient.UploadString(Uri address, String method, String data)
at System.Net.WebClient.UploadString(String address, String data)
at SensorsDaemonDLL.SensorDriver.Poll_iSDTC(Object sender, ElapsedEventArgs e)"
另一次尝试:
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ProtocolVersion = HttpVersion.Version10;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.ContentLength = cmdbytes.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(cmdbytes, 0, cmdbytes.Length);
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
int foo = 2;
}
request.GetResponse()引发异常:
例:信息:
"The server committed a protocol violation. Section=ResponseStatusLine"
"The server committed a protocol violation. Section=ResponseStatusLine"
设备可能没有发送回有效的响应。有没有可能使用像Fiddler这样的工具,然后从您的设备上发回响应 或者,您可以尝试以下解决方案之一:
一些用户提到了该链接中建议的一种解决方案对他们有效。您尝试的每一件事情都会完全符合您的要求。您的问题是什么?httpget是一个使用类似于HttpClient和HttpWebRequest的类和函数的程序。没有一个函数可以做与程序相同的事情。使用这些类来创建一个类似的程序什么东西不适用于WebClient?看,我确实看到了关于异常的问题。我已经尝试了那里的一切,似乎没有任何改变的行为。还有其他想法吗?我已经在阅读了很多帖子的基础上尽我所能尝试了。我认为欧米茄装置只是一个POS。