views:

1253

answers:

2

I'm trying to improve the information provided in response to an error handled within an app.

This is the code:

Try
        httpRequestObj = HttpWebRequest.Create(strRequest)
        httpRequestObj.Method = "GET"
        httpRequestObj.UseDefaultCredentials = True
*       httpResponse = httpRequestObj.GetResponse
        Using reader As StreamReader = New StreamReader(httpResponse.GetResponseStream())
            strXML = reader.ReadToEnd()
        End Using
    Catch ex As WebException
        'do something with ex
    End Try

The webexception is thrown on the * line

Currently all I see in the Exception is "The remote server returned an error: (500) Internal Server Error". I've looked at the exception in debug but the info I need isn't there- I guess the response would need to be read in to see that info but it never gets that far.

If I take the request and paste it into my browser directly I can see the error details in XML format that is returned from the API I'm calling, info like:

<Error>
  <description>info I want to get to here</description> 
  <detail /> 
  <code>info I want to get to here</code> 
  <source /> 
  <category>info I want to get to here</category> 
  <file>info I want to get to here</file> 
  <line>info I want to get to here</line> 
  <pad /> 
</Error>

Is there any way I can change this code so that I can get past the 500 error and see the actual response, I'd like to be able to parse this xml to find out the real problem for the failure.

Note: the Exception does have an ex.Response (System.Net.HttpWebResponse), but I can't see the info I need in there, only a load of Header info.

A: 

Try to use Fiddler. It's debuging proxy, which will show you all data sending between client and server. You'll be able to see all headers and context as well.

Ivan Nevostruev
good idea, and thanks for the quick answer
DannykPowell
@DannykPowell: This is must have for web developers.
Ivan Nevostruev
lol, yes I am well acquainted. And I agree
DannykPowell
+1  A: 

You can get the error response from the exception....

try
{
....
} catch(Exception e) {
   if (e is WebException && ((WebException)e).Status==WebExceptionStatus.ResponseError)
   {
      WebResponse errResp = ((WebException)e).Response;
      using(Stream respStream = errResp.GetResponseStream())
      {
         // read the error response
      }
   }
}
feroze
Just taking a look at this now. First problem hit with this code is that "ResponseError" does not appear to be part of the WebExceptionStatus enum http://msdn.microsoft.com/en-us/library/system.net.webexceptionstatus.aspx
DannykPowell
Marking this as correct in spite of that, as this answer did help me get to a solution. My code (vb.net) looks like: Catch ex As WebException Using reader As StreamReader = New StreamReader(ex.Response.GetResponseStream()) strXML = reader.ReadToEnd() End Using
DannykPowell
Sorry about that, it should actually be WebExceptionStatus.ProtocolError. In this case, the response object should have the error stream.Now, inspite of this, if your error stream is empty, then it might mean that the server either didnt send anything, or there is a bug somewhere. Can you get a tracelog using http://ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.html ? That willl show if the server sent a response entity or not.
feroze
InnerException?
Junto