有人可以幫我在這裏。我已經嘗試了各種.NET的東西:如何:在C#.NET中的httpget
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/2.0 (Win95; I)
Pragma: no-cache
Host: isd-9138:2000
Accept: */*
POSTING 6 bytes...
0027.3
C:\Users\Alexm>
如何在c#中簡單地做到這一點?
謝謝!
FYI:
C:\Users\Alexm>httpget -?
usage: httpget [options] URL
options:
-d - set debug mode
-q - set quiet mode (no error printouts)
-f - force data read on bad HTML
-h - add HTTP result header to output stream
-r - raw mode (no HTTP headers)
-s host[:port] - use SOCKS server "host" (optional port)
-p host[:port] - use proxy server "host" (optional port)
-C secs - set network read timeout in seconds
-t secs - set network connect timeout in seconds
-P file - POST file to URL (use "-" for stdin)
-S string - POST string to URL (expands \r and \n)
-N num - send POST file in multiple "num" byte chunks
-m - map special characters in POST data (\ quotes)
-c cookie - send cookie string with request
-R referringURL - send referring URL with request
-g generic - include specified generic header
-o output - send output to file "output" (default is stdout)
where URL is of the form "http://host[:port]/path"
C:\Users\Alexm>
例如這裏是一個嘗試:
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);
}
ex.Message:
"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()拋出異常:
ex.Message:
"The server committed a protocol violation. Section=ResponseStatusLine"
你嘗試過的每一件事情都會按照你的意願去做。你有什麼問題? – SLaks
httpget是一個程序,它使用*類和函數類似於HttpClient和HttpWebRequest。沒有*功能*與*程序*具有相同的功能。使用這些類來創建一個類似的程序 –
然後什麼不適用於WebClient? – rene