登录
原创

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

专栏C#
发布于 2020-10-23 阅读 2115
  • C#
原创

第一种异常: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异常,都可以用这种办法来获取服务器的返回内容。

评论区

king
5粉丝

励志做一条安静的咸鱼,从此走上人生巅峰。

0

0

0

举报