views:

1303

answers:

2

I'm communicating with a web server from .Net. The web server throws a 500 internal server error and writes a detailed error message.

I'm trying to read the error message that is received from a web exception, but getting another web exception. Why is the second WebException thrown?

try
{
  var webResponse = (HttpWebResponse)webRequest.GetResponse();
}
catch (WebException e)
{
  if (e.Status == WebExceptionStatus.ProtocolError)
  {
    // the next line throws a web exception
    Console.WriteLine(new StreamReader(e.Response.GetResponseStream()).ReadToEnd());
  }
}
+1  A: 

Why is this surprising? Try the following from MSDN:

try {
   // Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
     HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("invalid site");

    // Get the associated response for the above request.
     using (HttpWebResponse myHttpWebResponse = 
               (HttpWebResponse) myHttpWebRequest.GetResponse()) {
        myHttpWebResponse.Close();
    }
}
catch(WebException e) {
    Console.WriteLine("This program is expected to throw WebException on successful run."+
                        "\n\nException Message :" + e.Message);
    if(e.Status == WebExceptionStatus.ProtocolError) {
        var response = ((HttpWebResponse)e.Response);
        Console.WriteLine("Status Code : {0}", response.StatusCode);
        Console.WriteLine("Status Description : {0}", response.StatusDescription);

        try {
            using (var stream = response.GetResponseStream()) {
            using (var reader = new StreamReader(stream)) {
                var text = reader.ReadToEnd();
                Console.WriteLine(text);
            }
            }
        } catch (WebException ex) {
            // Oh, well, we tried
        }
    }
}
catch(Exception e) {
    Console.WriteLine(e.Message);
}
John Saunders
Amm, because I want to actually read the error message transmitted over the wire somehow. The website is not non-existent, it replies with an error, which I want to log / analyze on the client side.
ripper234
If the web site is nonexistant, where's the error come from? At any rate, updated.
John Saunders
It's _not_ non-existent :)
ripper234
And the update still doesn't help much - I don't understand why I'm getting an exception in the catch block.
ripper234
Interesting. I missed the "non". Please post the FULL exception you get in the catch block. Post the results of ex.ToString(). Also, if you implemented the code above, what's the value of ((HttpWebResponse)e.Response).StatusCode and .StatusDescription?
John Saunders
A: 

Looks like you're calling HTTPwebRequest from within the catch block.

If you call the same item that caused an exception from within the exception handling it will break again.

Hooloovoo
-1: Look more closely.
John Saunders
Think I'm still missing something. VB in .Net framework 2 doesn't even allow the e.GetResponseStream method call. Try Dim var As WebResponse = CType(req.GetResponse(), HttpWebResponse) Catch e As WebException If e.Status = WebExceptionStatus.ProtocolError Then Console.WriteLine(New StreamReader(e.GetResponseStream()).ReadToEnd()) End If End Trye.Response.GetResponseStream() is valid, however.I'll shut up now until the full exception is posted. :-)
Hooloovoo
I corrected my post to use e.Response.GetResponseStream. It was a typo.
John Saunders