1

I'm catching an exception, which I've done in two ways. With the first method, I'm catching the full exception, checking to see if the inner exception is of type WebException, and if it is, obtain the response stream. Below is the first example, however I always get a zero-string response:

catch (Exception e)
{
    if (e.InnerException is WebException)
    {
        WebException webEx = (WebException)e.InnerException;
        HttpWebResponse myResponse = webEx.Response as HttpWebResponse;
        string response = string.Empty;

        if (myResponse != null)
        {
            StreamReader strm = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            response = strm.ReadToEnd();
            //EMPTY RESPONSE
        }
    }
}

However, if I catch the Web Exception, and pretty much do the same thing, I obtain the response fine:

catch (WebException e)
{
    HttpWebResponse myResponse = e.Response as HttpWebResponse;
    string response = string.Empty;

    if (myResponse != null)
    {
        StreamReader strm = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
        response = strm.ReadToEnd();
        //POPULATED RESPONSE
    }
}

Any ideas why I'm able to parse the response in the second example but not in the first?

Erwin
  • 4,757
  • 3
  • 31
  • 41
bgeveritt
  • 303
  • 5
  • 18
  • What's the type of 'e' (the outer exception) in your first example? I think Hans Passant's comment in this question, http://stackoverflow.com/questions/6479771/properties-of-inner-exception-are-disposed, is a likely reason, however. – dash Jun 04 '12 at 20:45

2 Answers2

0

Don't look at the InnerException, in your second example you're reading the response from the exception you caught, that's why it works. Just change it to this, should work fine:

catch (Exception e)
{
  if (e is WebException)
  {
      WebException webEx = (WebException)e;
      HttpWebResponse myResponse = webEx.Response as HttpWebResponse;
      string response = string.Empty;

      if (myResponse != null)
      {
          StreamReader strm = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
          response = strm.ReadToEnd();
      }
  }
}
Erwin
  • 4,757
  • 3
  • 31
  • 41
greg84
  • 7,541
  • 3
  • 36
  • 45
-1

Don't check the InnerException, it is the Exception instance that caused the current exception (From MSDN)

Just perform a check on Exception -

        catch (Exception e)
        {
            if (e is WebException)
            {
                WebException webEx = (WebException)e.InnerException;
                HttpWebResponse myResponse = webEx.Response as HttpWebResponse;
                string response = string.Empty;

                if (myResponse != null)
                {
                    StreamReader strm = new  StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                    response = strm.ReadToEnd();
                    //EMPTY RESPONSE
                }
            }
        }

Hope it helps !!

Prateek Singh
  • 863
  • 1
  • 8
  • 28