HttpWebRequest throws exception, but HaveResponse is true?

  • Thread starter Thread starter Shell
  • Start date Start date
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.
 
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
 
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.
 
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.
 
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
 
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.
----------------
 
Back
Top