Home > Uncategorized > Get details of a WebException in C=

Get details of a WebException in C=

If a HTTP call fails with a HTTP 500 error, and throws a WebException, you know something’s gone wrong, but sometimes the response from the server can explain more about what screwed up. I found myself writing this code over and over again, so here’s a single method that handles it;

private static string GetExceptionDetails(WebException exception)
{
string strResult = “”;
if (exception.Response != null)
{
var responseStream = exception.Response.GetResponseStream();
if (responseStream != null)
{
using (var reader = new StreamReader(responseStream))
{
strResult = reader.ReadToEnd();
}
}
}
return strResult;
}

Hope this helps solve your web related problems!

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment