HttpWebRequest 在出错时获取response内容

HttpWebRequest 请求时,服务器会返回500  501这些错误 并包含错误信息,通过如下代码可以拿到错误信息

HttpWebResponse res;
try
{
res = (HttpWebResponse)req.GetResponse();
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
}
StreamReader sr = new StreamReader(res.GetResponseStream(), strEncode);
strHtml = sr.ReadToEnd();
原文地址:https://www.cnblogs.com/bfyx/p/7966239.html