In Fiddler, I can pass in a request and get a response 500. Fine by me, I want my .NET code to handle this AND analyze the response message. In the case below there is a meaningful message. Yet using HttpWebRequest and HttpWebResponse, I cannot get that information.
Here is the response from Fiddler:
HTTP/1.1 500 Internal Server Error
Date: Sun, 25 Mar 2012 15:50:31 GMT
Transfer-Encoding: chunked
Content-Type: text/xml; charset=ISO-8859-1
X-Powered-By: Servlet/2.5 JSP/2.1
010e
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>WSS header is missing from request. Can't do username token authentication.</faultstring></soap:Fault></soap:Body></soap:Envelope>
0000
Here is my code which tries to catch the exception:
try
{
HttpWebResponse resp2 = (HttpWebResponse) req.GetResponse();
}
catch (WebException ex)
{
WebException we = (WebException) ex;
HttpWebResponse respp = (HttpWebResponse) we.Response;
log.Info((int)respp.StatusCode);
log.Error(ex.ToString());
}
I just cannot get anything more from the exception object. But I know the response should be readable. Hopefully there are lower-level http classes I can use that I just don’t know about now. I don’t want .NET protecting me from the real HTTP protocols.
It’s not clear what you’ve tried to do with
respp. (I note that you’re not usingresp2, either.) For example, what does this do:(It’s never been clear to me whether you really need to dispose of a
WebResponseobtained in aWebException, but it’s probably a good idea. You might want to dispose of the response stream as well, although I believe disposing of the response is good enough.)