无法获得实际错误以响应REST请求

无法获得实际错误以响应REST请求

本文介绍了无法获得实际错误以响应REST请求.Fiddler向我显示了更多错误详细信息.在.NET中,我得到的只是500-内部服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中,我正在调用Rest服务并收到异常- 500 Internal Server Error .

In .NET, I am calling a rest service and getting an exception - 500 Internal Server Error.

HttpWebResponse response = request.GetResponse() as HttpWebResponse

当我在Fiddler(在TextView中)分析此问题时,我得到了许多有关导致错误的适当异常的详细信息.

When I analyze this in Fiddler (in TextView), I am getting many details about the proper exception that caused the error.

在我的异常对象中,无法在 InnerException (它为空)或 Response 对象本身中获取此信息.

In my exception object, I can't get this information in the InnerException (it's null) nor in the Response object itself.

有什么想法吗?

推荐答案

尝试查看 WebException.Response属性:

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());
                }
            }
        }
    }
}

这篇关于无法获得实际错误以响应REST请求.Fiddler向我显示了更多错误详细信息.在.NET中,我得到的只是500-内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:50