HttpWebRequest 在出错时获取response内容

Posted 冰封一夏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();

以上是关于HttpWebRequest 在出错时获取response内容的主要内容,如果未能解决你的问题,请参考以下文章