Can't get actual error in response for REST re

2019-07-03 16:54发布

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?

标签: c# rest Fiddler
2条回答
▲ chillily
2楼-- · 2019-07-03 17:29

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());
                }
            }
        }
    }
}
查看更多
We Are One
3楼-- · 2019-07-03 17:39

The "inner" exception is happening on the remote host. The WebException has a Response property which you can read to get the details.

查看更多
登录 后发表回答