Using HttpWebRequest and HttpWebResponse

  • Thread starter Thread starter Zoe Hart
  • Start date Start date
Z

Zoe Hart

I maintaining or trying to maintain a relatively simply HttpHandler. The
idea is that it takes a request to http://myhandler/url/someURL and creates
a request to http://someaddress/someresource. When it gets the response, it
streams that response back to the original incoming request. So it's
basically just mapping url/someURL to someaddress/someresource, with the
mapping information being supplied in the handler's config file.
Within the HttpHandler code, we're using HttpWebRequest to create the
internal request to the mapped resource and HttpWebResponse to capture the
response that we then stream back as the response to the original request.
This works well when the HttpWebRequest generates an HTTP 200 response. When
the generated HttpWebRequest results in an Http 4xx or 5xx, instead of
getting the actual response streamed back to the original request, I'm
getting an HTTP 500 with the message "The remote server returned an error:
(xxx)" where xxx is the HTTP status code of the response to the internally
generated request. I assume that either HttpWebRequest or HttpWebResponse is
doing this wrapping of the original response. How can I get the actual
response code and content to the internally generated request streamed back
to the original request instead of this wrapper.

Thanks,
Zoe
 
Thanks for all the responses... not.

I got around this by wrapping the HttpWebRequest.GetResponse call in a
Try-Catch block and catching the System.Net.WebException that gets thrown
when an HTTP 4xx or 5xx status is returned. Then I set my HttpWebResponse
variable to WebException.Response - the original response. If you don't
catch and handle this exception, GetResponse returns an HTTP 500 response
with content that says "The remote server returned xxx" where xxx is the 4xx
or 5xx code of the original response - but in this case you've lost the
content that went with that response. By catching the exception I was able
to get the original response with its content that included XML I needed.

Zoe
 
Back
Top