ResponseStatusLine protocol violation

Posted by Tom Hines on Geeks with Blogs See other posts from Geeks with Blogs or by Tom Hines
Published on Fri, 03 Dec 2010 17:10:21 GMT Indexed on 2010/12/06 16:57 UTC
Read the original article Hit count: 380

Filed under:

I parse/scrape a few web page every now and then and recently ran across an error that stated:
"The server committed a protocol violation. Section=ResponseStatusLine".

Exception

 

After a few web searches, I found a couple of suggestions – one of which said the problem could be fixed by changing the HttpWebRequest ProtocolVersion to 1.0 with the command:

   1:  HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strURI);
   2:  req.ProtocolVersion = HttpVersion.Version10;

 

ProtocolVersion

…but that did not work in my particular case.

 

What DID work was the next suggestion I found that suggested the use of the setting: “useUnsafeHeaderParsing” either in the app.config file or programmatically.

If added to the app.config, it would be:

   1:  <!-- after the applicationSettings -->
   2:  <system.net>
   3:      <settings>
   4:        <httpWebRequest useUnsafeHeaderParsing ="true"/>
   5:      </settings>
   6:  </system.net>

 

If done programmatically, it would look like this:


C++:

   1:  // UUHP_CPP.h
   2:  #pragma once
   3:  using namespace System;
   4:  using namespace System::Reflection;
   5:   
   6:  namespace UUHP_CPP
   7:  {
   8:      public ref class CUUHP_CPP
   9:      {
  10:      public:
  11:          static bool UseUnsafeHeaderParsing(String^% strError)
  12:          {
  13:              Assembly^ assembly = Assembly::GetAssembly(System::Net::Configuration::SettingsSection::typeid); //__typeof
  14:              if (nullptr==assembly)
  15:              {
  16:                  strError = "Could not access Assembly";
  17:                  return false;
  18:              }
  19:   
  20:              Type^ type = assembly->GetType("System.Net.Configuration.SettingsSectionInternal");
  21:              if (nullptr==type)
  22:              {
  23:                  strError = "Could not access internal settings";
  24:                  return false;
  25:              }
  26:   
  27:              Object^ obj = type->InvokeMember("Section",
  28:                  BindingFlags::Static | BindingFlags::GetProperty | BindingFlags::NonPublic,
  29:                  nullptr, nullptr, gcnew array<Object^,1>(0));
  30:   
  31:              if(nullptr == obj)
  32:              {
  33:                  strError = "Could not invoke Section member";
  34:                  return false;
  35:              }
  36:   
  37:              FieldInfo^ fi = type->GetField("useUnsafeHeaderParsing", BindingFlags::NonPublic | BindingFlags::Instance);
  38:              if(nullptr == fi)
  39:              {
  40:                  strError = "Could not access useUnsafeHeaderParsing field";
  41:                  return false;
  42:              }
  43:   
  44:              if (!(bool)fi->GetValue(obj))
  45:              {
  46:                  fi->SetValue(obj, true);
  47:              }
  48:   
  49:              return true;
  50:          }
  51:      };
  52:  }

C# (CSharp):

   1:  using System;
   2:  using System.Reflection;
   3:   
   4:  namespace UUHP_CS
   5:  {
   6:     public class CUUHP_CS
   7:     {
   8:        public static bool UseUnsafeHeaderParsing(ref string strError)
   9:        {
  10:           Assembly assembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
  11:           if (null == assembly)
  12:           {
  13:              strError = "Could not access Assembly";
  14:              return false;
  15:           }
  16:   
  17:           Type type = assembly.GetType("System.Net.Configuration.SettingsSectionInternal");
  18:           if (null == type)
  19:           {
  20:              strError = "Could not access internal settings";
  21:              return false;
  22:           }
  23:   
  24:           object obj = type.InvokeMember("Section",
  25:              BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic,
  26:              null, null, new object[] { });
  27:   
  28:           if (null == obj)
  29:           {
  30:              strError = "Could not invoke Section member";
  31:              return false;
  32:           }
  33:   
  34:           // If it's not already set, set it.
  35:           FieldInfo fi = type.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
  36:           if (null == fi)
  37:           {
  38:              strError = "Could not access useUnsafeHeaderParsing field";
  39:              return false;
  40:           }
  41:   
  42:           if (!Convert.ToBoolean(fi.GetValue(obj)))
  43:           {
  44:              fi.SetValue(obj, true);
  45:           }
  46:   
  47:           return true;
  48:        }
  49:     }
  50:  }

 


F# (FSharp):

   1:  namespace UUHP_FS
   2:  open System
   3:  open System.Reflection
   4:      module CUUHP_FS =
   5:          let UseUnsafeHeaderParsing(strError : byref<string>) : bool  =
   6:              //
   7:              let assembly : Assembly = Assembly.GetAssembly(typeof<System.Net.Configuration.SettingsSection>)    
   8:              if (null = assembly) then
   9:                  strError <- "Could not access Assembly"
  10:                  false
  11:              else
  12:              
  13:              let myType : Type = assembly.GetType("System.Net.Configuration.SettingsSectionInternal")
  14:              if (null = myType) then
  15:                  strError <- "Could not access internal settings"
  16:                  false
  17:              else
  18:              
  19:              let obj : Object = myType.InvokeMember("Section", BindingFlags.Static ||| BindingFlags.GetProperty ||| BindingFlags.NonPublic, null, null, Array.zeroCreate 0)
  20:              if (null = obj) then
  21:                  strError <- "Could not invoke Section member"
  22:                  false
  23:              else
  24:              
  25:              // If it's not already set, set it.
  26:              let fi : FieldInfo = myType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic ||| BindingFlags.Instance)
  27:              if(null = fi) then
  28:                  strError <- "Could not access useUnsafeHeaderParsing field"
  29:                  false
  30:              else
  31:              
  32:              if (not(Convert.ToBoolean(fi.GetValue(obj)))) then
  33:                  fi.SetValue(obj, true)
  34:                  
  35:              // Now return true
  36:              true           

VB (Visual Basic):

   1:  Option Explicit On
   2:  Option Strict On
   3:  Imports System
   4:  Imports System.Reflection
   5:   
   6:  Public Class CUUHP_VB
   7:      Public Shared Function UseUnsafeHeaderParsing(ByRef strError As String) As Boolean
   8:   
   9:          Dim assembly As [Assembly]
  10:          assembly = [assembly].GetAssembly(GetType(System.Net.Configuration.SettingsSection))
  11:   
  12:          If (assembly Is Nothing) Then
  13:              strError = "Could not access Assembly"
  14:              Return False
  15:          End If
  16:   
  17:          Dim type As Type
  18:          type = [assembly].GetType("System.Net.Configuration.SettingsSectionInternal")
  19:          If (type Is Nothing) Then
  20:              strError = "Could not access internal settings"
  21:              Return False
  22:          End If
  23:   
  24:          Dim obj As Object
  25:          obj = [type].InvokeMember("Section", _
  26:              BindingFlags.Static Or BindingFlags.GetProperty Or BindingFlags.NonPublic, _
  27:          Nothing, Nothing, New [Object]() {})
  28:   
  29:          If (obj Is Nothing) Then
  30:              strError = "Could not invoke Section member"
  31:              Return False
  32:          End If
  33:   
  34:          ' If it's not already set, set it.
  35:          Dim fi As FieldInfo
  36:          fi = [type].GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic Or BindingFlags.Instance)
  37:          If (fi Is Nothing) Then
  38:              strError = "Could not access useUnsafeHeaderParsing field"
  39:              Return False
  40:          End If
  41:   
  42:          If (Not Convert.ToBoolean(fi.GetValue(obj))) Then
  43:              fi.SetValue(obj, True)
  44:          End If
  45:   
  46:          Return True
  47:      End Function
  48:  End Class

 

© Geeks with Blogs or respective owner