誰かが私をここで助けてくれますか?私はあらゆる種類の.NETを試しました。方法:C#でのhttpget
WebClient、HttpWebResponse、WebResponseなど... Webからのさまざまな提案。
デバイスから値を読み取ろうとしています。これは私がそれをする方法です:
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:
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)"
ここで例えば
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>
は1つの試みであります
別の試み:
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