最佳答案
我正在启动一个 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)."
}
如果你能帮忙,我将不胜感激。