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();
posted @ 2017-12-03 15:39  冰封一夏  阅读(3923)  评论(0编辑  收藏  举报
HZHControls控件库官网:http://hzhcontrols.com