问题:

我正在初始化HttpWebRequest,然后检索它的响应。 有时,我得到500(或至少5 ##)错误,但没有描述。 我对两个端点都有控制权,希望接收端能获得更多信息。 例如,我想将异常消息从服务器传递到客户端。 使用HttpWebRequest和HttpWebResponse是否可能?

码:

try
{
    HttpWebRequest webRequest = HttpWebRequest.Create(URL) as HttpWebRequest;
    webRequest.Method = WebRequestMethods.Http.Get;
    webRequest.Credentials = new NetworkCredential(Username, Password);
    webRequest.ContentType = "application/x-www-form-urlencoded";
    using(HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
    {
        if(response.StatusCode == HttpStatusCode.OK)
        {
            // Do stuff with response.GetResponseStream();
        }
    }
}
catch(Exception ex)
{
    ShowError(ex);
    // if the server returns a 500 error than the webRequest.GetResponse() method
    // throws an exception and all I get is "The remote server returned an error: (500)."
}

任何帮助,将不胜感激。

I am initiating an HttpWebRequest and then retrieving it’s response.Occasionally, I get a 500 (or at least 5##) error, but no description.I have control over both endpoints and would like the receiving end to get a little bit more information.For example, I would like to pass the exception message from server to client.Is this possible using HttpWebRequest and HttpWebResponse?Code:Any help with this would be much appreciated.