Reputation: 9324
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.
Upvotes: 0
Views: 3814
Reputation: 28325
I think you can catch the SoapException instead.
try
{
// do your thing
}
catch (SoapException soapEx)
{
MessageBox.Show(soapEx.Code.ToString());
//Load the Detail element of the SoapException object
XmlDocument doc = new XmlDocument();
doc.LoadXml(soapEx.Detail.OuterXml);
XmlNamespaceManager nsManager = new
XmlNamespaceManager(doc.NameTable);
// Add the namespace to the NamespaceManager
nsManager.AddNamespace("errorNS",
"WSSoapException");
XmlNode Node =
doc.DocumentElement.SelectSingleNode("errorNS:Error",
nsManager);
string errorNumber =
Node.SelectSingleNode("errorNS:ErrorNumber",
nsManager).InnerText;
string errorMessage =
Node.SelectSingleNode("errorNS:ErrorMessage",
nsManager).InnerText;
string errorSource =
Node.SelectSingleNode("errorNS:ErrorSource",
nsManager).InnerText;
MessageBox.Show("Error Number is " + errorNumber);
MessageBox.Show("Error Message is " + errorMessage);
MessageBox.Show("Error Source is " + errorSource);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Upvotes: 0
Reputation: 1503964
It's not clear what you've tried to do with respp
. (I note that you're not using resp2
, either.) For example, what does this do:
catch (WebException ex)
{
using (HttpWebResponse respp = (HttpWebResponse) ex.Response)
{
log.Info((int)respp.StatusCode);
log.Error(ex.ToString());
log.Error(new StreamReader(respp.GetResponseStream()).ReadToEnd());
}
}
(It's never been clear to me whether you really need to dispose of a WebResponse
obtained in a WebException
, 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.)
Upvotes: 3