1

I am attempting to retrieve data from an HTTP handler to my C# app in a similar fashion to Chromes Postman and Advanced rest client.

Eg

Target = http://10.113.171.82/handler
Payload =
"<?xml version="1.0" encoding="utf-8"?>
<webpage pageID="406" cmdID="GET" cursor="0"/>"

This works fine from within Postman or Advanced Rest Client but fails miserably from a C# app. The latest error is
The server committed a protocol violation. Section=ResponseStatusLine.

I've tried the usual suggestions (KeepAlive False, useUnsafeHeaderParsing="true").

Here's the abbreviated version.

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://10.113.171.82/handler");
string payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?><webpage pageID=\"406\" cmdID=\"GET\" cursor=\"0\"/>";
byte[] data = Encoding.GetEncoding("iso-8859-1").GetBytes(payload);
myHttpWebRequest.ContentLength = data.Length;
myHttpWebRequest.KeepAlive = false;
CookieContainer cookieJar = new CookieContainer();
myHttpWebRequest.ProtocolVersion = HttpVersion.Version11;
myHttpWebRequest.CookieContainer = cookieJar;
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.AllowAutoRedirect = true;
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)";
myHttpWebRequest.Timeout = 100000;
myHttpWebRequest.Accept = "text/xml; charset=utf-8";
myHttpWebRequest.Method = "POST";
myHttpWebRequest.ServicePoint.Expect100Continue = false;
myHttpWebRequest.ServicePoint.MaxIdleTime = 100000;
myHttpWebRequest.KeepAlive = false;
myHttpWebRequest.ContentType = "text/xml; charset=utf-8";
Stream requestStream = myHttpWebRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
myHttpWebRequest.AllowWriteStreamBuffering = true;

//Error on the GetResponse
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

Stream responseStream = myHttpWebResponse.GetResponseStream();

StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);

pageContent = myStreamReader.ReadToEnd();

myStreamReader.Close();
responseStream.Close();

myHttpWebResponse.Close();

Things to note. If I remove the URLEncoding the error is "The underlying connection was closed: An unexpected error occurred on a receive." Although this seems a bit hit and miss. Also, if I remove /handler from the target I get an HTML response but not what I want. Most of the properties have been added in an attempt to find a solution. I think I'm missing something fundamental!

1 Answer 1

1

I think I've discovered the problem. The server returns no headers at all. If I enable source debugging I find a null reference exception at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)

Although this resolves this problem (why it's happening) it simply creates a new problem. How to receive an HTTP request without HTTP/1.0 200 OK

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.