HttpWebRequest throws exception, but HaveResponse is true?

S

Shell

I'm using HttpWebRequest to create a request and get a chart from a web
site. It works great unless one of my request parameters is wrong.

i.e., if I say http://www.xyz.com/x?a=b it's fine, but if instead of
x?a=b, I do x?a=c, I get a protocol violation error (The server has
closed the connection). This is odd because if I do the same thing from
IE, I get an image which says "Chart not available".

Also, the HaveResponse property says 'true'. Is there any way I can get
whatever response was returned even if there was a protocol violation?
Any other workarounds?

Thanks in advance.
 
R

Robert Jordan

Hi,
I'm using HttpWebRequest to create a request and get a chart from a web
site. It works great unless one of my request parameters is wrong.

i.e., if I say http://www.xyz.com/x?a=b it's fine, but if instead of
x?a=b, I do x?a=c, I get a protocol violation error (The server has
closed the connection). This is odd because if I do the same thing from
IE, I get an image which says "Chart not available".

Also, the HaveResponse property says 'true'. Is there any way I can get
whatever response was returned even if there was a protocol violation?
Any other workarounds?

from the WebException:

try {
// do HttpWebRequest
}
catch (WebException ex) {
if (e.Status == WebExceptionStatus.ProtocolError) {
HttpWebResponse r = (HttpWebResponse) e.Respose;
// do something with the response
}
else {
throw;
}
}

bye
Rob
 
J

Jon Skeet [C# MVP]

Shell said:
I'm using HttpWebRequest to create a request and get a chart from a web
site. It works great unless one of my request parameters is wrong.

i.e., if I say http://www.xyz.com/x?a=b it's fine, but if instead of
x?a=b, I do x?a=c, I get a protocol violation error (The server has
closed the connection). This is odd because if I do the same thing from
IE, I get an image which says "Chart not available".

Also, the HaveResponse property says 'true'. Is there any way I can get
whatever response was returned even if there was a protocol violation?
Any other workarounds?

You can get the response from the WebException's Response property.
 
S

Shell

Interesting idea, but it doesn't seem to work.

e.Response is null, but request.HaveResponse is still true! For the
moment, I've gotten it to work and show the right "Chart not available"
message by setting useUnsafeHeaderParsing = true in the app.config
file. It's a hack, and I'm not sure what implications it will have, but
it seems to be the only thing that works. I read somewhere that this is
supposed to work only on .net 1.1 sp1.

I checked the headers being returned, and sure enough, there's an ugly
"HTTP/1.1 200 OK" that's causing this problem.
 
W

Wessel

I checked the headers being returned, and sure enough, there's an ugly
"HTTP/1.1 200 OK" that's causing this problem.

Same problem here, but I wonder, what's wrong about "HTTP/1.0 200 OK" ?

Setting useUnsafeHeaderParsing seems to solve my problem, tho.

Regards,
Wessel
 
F

Feroze [msft]

In V2 & V1.1/Sp1 of the framework, we tightened the rules on header formats
that we will accept. So, if the server is sending malformed headers, you
will get a ProtocolViolation error.

You need to run a network sniffer to see what is the problem in the response
that the server is sending.

--
feroze

-----------------
This posting is provided as-is. It offers no warranties and assigns no
rights.

See http://weblogs.asp.net/feroze_daud for System.Net related posts.
----------------
 

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

Top