problem with HttpWebRequest GetResponse

  • Thread starter Thread starter bg
  • Start date Start date
B

bg

Hi all.

I 'm trying to automate some of my workflow, by doing some programmatic
"POST"s to an internal webserver via HttpWebRequest. The Code is your usual
straight forward type of thing.

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType="application/x-www-form-urlencoded";
req.ContentLength = data.Length;
Stream newStream=req.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();
reponse = req.GetResponse() as HttpWebResponse; <===goes bang here

But on the GetResponse I get an "The underlying connection was closed: The
server committed an HTTP protocol violation." exception.

Now the thing is if I use the excellent TcpTrace utility to trace whats
going over and coming back I can see that valid data is being retrieved from
the call, So how can I ignore the exception and just get to the response
data? (HaveResponse is true, but response ==null)

Or perhaps there's a flag i'm not setting somewhere?

TIA.

bg
 
What exactly is in "data"? I know it's in byte array format, but what is it
in string format?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
What exactly is in "data"? I know it's in byte array format, but what
is it in string format?

its the name/value pairing stuff thats normally supplied by the form,

property1=value&property2=value.....

I took the data straight from a trace I had and still got the same exception.
 
It is possible that the server is actually sending an illegal HTTP response.
This happens with some servers. You can change a configuration setting in
the system.net configuration section (httpWebRequest element) like so, to
allow unsafe header parsing:

<configuration>
<system.net>
<settings>
<httpWebRequest
useUnsafeHeaderParsing="true"
/>
</settings>
</system.net>
</configuration>

It may fix the problem.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
Thus wrote BG,
Hi all.

I 'm trying to automate some of my workflow, by doing some
programmatic "POST"s to an internal webserver via HttpWebRequest. The
Code is your usual straight forward type of thing.

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType="application/x-www-form-urlencoded";
req.ContentLength = data.Length;
Stream newStream=req.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();
reponse = req.GetResponse() as HttpWebResponse; <===goes bang here
But on the GetResponse I get an "The underlying connection was closed:
The server committed an HTTP protocol violation." exception.

Now the thing is if I use the excellent TcpTrace utility to trace
whats going over and coming back I can see that valid data is being
retrieved from the call, So how can I ignore the exception and just
get to the response data? (HaveResponse is true, but response ==null)

Can you post a sample HTTP response? My usual answer is "fix the broken server"
;-)

Cheers,
 
Added "useUnsafeHeaderParsing="true"" to my app.config and it now works a
treat - thanks Kevin
 
Back
Top