这是我的代码:
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
WebResponse wr = req.GetResponse();
当服务器返回500 Internal Server Error时,req.GetResponse()中会引发异常。
我希望GetResponse()接受此响应代码,传递的URL抛出此响应代码是正常的。尽管响应代码500 Internal Server Error,我仍想解析HTML。是否可以说GetResponse()方法不验证响应代码?
最佳答案
try
{
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
WebResponse wr = req.GetResponse();
}
catch (WebException wex)
{
var pageContent = new StreamReader(wex.Response.GetResponseStream())
.ReadToEnd();
}
关于c# - HttpWebRequest接受500内部服务器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18403846/