HttpWebRequest.GetResponse出异常时的解决办法


参考文章  http://www.cnblogs.com/cresuccess/archive/2009/12/09/1619977.html


第一种异常:WebException: 服务器提交了协议冲突

如果 将UseUnsafeHeaderParsing 属性设置为 true,则忽略验证错误。

C#代码如下:

static void Main()
        {
            // Enable UseUnsafeHeaderParsing
            if (!ToggleAllowUnsafeHeaderParsing(true))
            {
                // Couldn't set flag. Log the fact, throw an exception or whatever.
            }

            // This request will now allow unsafe header parsing, i.e. GetResponse won't throw an exception.
            var request = (HttpWebRequest) WebRequest.Create("http://localhost:8000");
            var response = request.GetResponse();

            // Disable UseUnsafeHeaderParsing
            if (!ToggleAllowUnsafeHeaderParsing(false))
            {
                // Couldn't change flag. Log the fact, throw an exception or whatever.
            }

            // This request won't allow unsafe header parsing, i.e. GetResponse will throw an exception.
            var strictHeaderRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8000");
            var strictResponse = strictHeaderRequest.GetResponse();
        }

        // Enable/disable useUnsafeHeaderParsing.
        // See http://o2platform.wordpress.com/2010/10/20/dealing-with-the-server-committed-a-protocol-violation-sectionresponsestatusline/
        public static bool ToggleAllowUnsafeHeaderParsing(bool enable)
        {
            //Get the assembly that contains the internal class
            Assembly assembly = Assembly.GetAssembly(typeof(SettingsSection));
            if (assembly != null)
            {
                //Use the assembly in order to get the internal type for the internal class
                Type settingsSectionType = assembly.GetType("System.Net.Configuration.SettingsSectionInternal");
                if (settingsSectionType != null)
                {
                    //Use the internal static property to get an instance of the internal settings class.
                    //If the static instance isn't created already invoking the property will create it for us.
                    object anInstance = settingsSectionType.InvokeMember("Section",
                    BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });
                    if (anInstance != null)
                    {
                        //Locate the private bool field that tells the framework if unsafe header parsing is allowed
                        FieldInfo aUseUnsafeHeaderParsing = settingsSectionType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
                        if (aUseUnsafeHeaderParsing != null)
                        {
                            aUseUnsafeHeaderParsing.SetValue(anInstance, enable);
                            return true;
                        }

                    }
                }
            }
            return false;
        }

或者也可以在App.config或者Web.config文件里面添加如下配置:

<system.net> 
  <settings> 
   <httpWebRequest useUnsafeHeaderParsing="true" /> 
  </settings> 
</system.net> 


第二种异常:WebException 服务器状态码错误,比如500服务器内部错误

现象

我们编码实现请求一个页面时,请求的代码类似如下代码:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl);
req.UserAgent = "MSIE6.0";
req.Method = "GET";
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), strEncode);
strHtml = sr.ReadToEnd();
sr.Close();
res.Close();

但是,如果我们请求的这个页面正好是一个有异常发生的页面,或者不存在的页面。我们上面的代码就会在

req.GetResponse();
这里抛出异常:远程服务器返回错误:  (500)   内部服务器错误。

我们通过上面的代码,是不能得到错误发生时候的页面源代码的。

 

分析原因

(HttpWebResponse)req.GetResponse(); 这行代码中做了如下一件事情:

当服务器段ASP.net程序有 Exception 发生时,客户端应用程序接受了HTTP 协议错误后。把这个HTTP 协议错误转换成 Status 设置为 WebExceptionStatus.ProtocolError 的 WebException,并且把这个异常throw出来。

 

解决问题

那如果我们想获得错误发生时候服务器段错误页面的源代码该如何做呢?

其实非常非常简单的做法,我们用下面的代码就不论错误发生与否,都可以获得服务器段页面的源代码。

 HttpWebResponse res;
try
{
res = (HttpWebResponse)req.GetResponse();
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
}
StreamReader sr = new StreamReader(res.GetResponseStream(), strEncode);
strHtml = sr.ReadToEnd();
当异常发生事后,WebException 中不仅有 StatusCode 标志着 HTTP 的错误代码,而且它的 Response 属性还包含由服务器发送的 WebResponse,来指示遇到的实际 HTTP 错误。

总结: 还是第二种解决办法比较通用,只要是WebException异常,都可以用这种办法来获取服务器的返回内容。


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值