In .NET, I am calling a rest service and getting an exception - 500 Internal Server Error
.
HttpWebResponse response = request.GetResponse() as HttpWebResponse
When I analyze this in Fiddler (in TextView), I am getting many details about the proper exception that caused the error.
In my exception object, I can't get this information in the InnerException
(it's null) nor in the Response
object itself.
Any ideas?
Try looking at the WebException.Response Property:
catch(WebException ex)
{
Console.WriteLine(ex.Message);
if (ex.Status == WebExceptionStatus.ProtocolError)
{
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)ex.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)ex.Response).StatusDescription);
using (Stream responseStream = ex.Response.GetResponseStream())
{
if (responseStream != null)
{
using (StreamReader responseReader = new StreamReader(responseStream))
{
Console.WriteLine(responseReader.ReadToEnd());
}
}
}
}
}
The "inner" exception is happening on the remote host. The WebException has a Response property which you can read to get the details.