-2

Can someone help me out here. I have tried all sorts of .NET things:

WebClient, HttpWebResponse, WebResponse, etc... Various suggestions from the web.

I am trying to read a value from a device. This is how I do it:

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>

How do I do this simply in c#?

Thanks!

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>

For example here is one attempt:

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"

Stack Trace:

"   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)"

Another attempt:

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() throws exception:

ex.Message:

"The server committed a protocol violation. Section=ResponseStatusLine"
Alex Martin
  • 156
  • 9
  • Each of the things you tried will do exactly what you want. What is your problem? – SLaks Sep 15 '17 at 15:49
  • httpget is a program that *uses* classes and functions similar to HttpClient and HttpWebRequest. There's no *function* that does the same thing as a *program*. Use the classes to create a similar program – Panagiotis Kanavos Sep 15 '17 at 15:50
  • What didn't work with the WebClient then? – rene Sep 15 '17 at 15:58
  • see https://stackoverflow.com/questions/2482715/the-server-committed-a-protocol-violation-section-responsestatusline-error – rene Sep 15 '17 at 15:59
  • I did see that question regarding the exception. I have tried everything in there, nothing seems to change the behavior. Any other ideas? – Alex Martin Sep 15 '17 at 16:50
  • I think it has something to do with the "raw mode". httpget crashes without the "-r" flag. Of course I can't find any documentation for the program. It comes from here: http://www.newportus.com/software/iServer.htm – Alex Martin Sep 15 '17 at 17:26
  • You can use a process class and get results from standard output just like you do from a cmd.exe. If you are using a WebClient then add the header " User-Agent: Mozilla/2.0 (Win95; I). See posting : https://stackoverflow.com/questions/11841540/setting-the-user-agent-header-for-a-webclient-request – jdweng Sep 15 '17 at 17:36

1 Answers1

0

The device might not be sending a valid response back. Will it be possible to use a tool like Fiddler and post the response back from your device?

Alternatively you could try one of the solutions from here: https://www.webmonkeys.org.uk/2012/09/c-the-server-committed-a-protocol-violation-sectionresponsestatusline/

Some users has mentioned one of the solutions suggested in that link has worked for them.

vjgn
  • 334
  • 1
  • 2
  • 8