problem with HttpWebRequest GetResponse

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
 
K

Kevin Spencer

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.
 
B

Barrie Green

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.
 
K

Kevin Spencer

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.
 
J

Joerg Jooss

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,
 
B

bg

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top