我正在初始化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)."
}
任何帮助,将不胜感激。