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?