IT Community - Software Programming, Web Development and Technical Support

"System.Net.WebException: The server committed a protocol violation.

This is a discussion on "System.Net.WebException: The server committed a protocol violation. within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi... i am getting the "System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine" exception, when ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 01-10-2008, 02:32 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default "System.Net.WebException: The server committed a protocol violation.

Hi...

i am getting the "System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine" exception, when i cantact a webservice using HttpWebRequest class...


This is the code.

Code:
                    HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
                    webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    webRequest.Accept = "text/xml";
                    webRequest.AllowAutoRedirect = true;
                    HttpWebResponse webResponse=null;
                    Stream s = null;
                    try
                    {
                        webResponse = (HttpWebResponse)webRequest.GetResponse();
                        s = webResponse.GetResponseStream();
                    }
                    catch
                    {
                        
                    }

Iam getting error in the try block...


thnx in advance...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-10-2008, 02:36 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Thumbs up Re: "System.Net.WebException: The server committed a protocol violation.

Hi,
One of the issues involving XML-RPC.NET that turns up fairly frequently is when the library throws an instance of System.Net.WebException with the message ""The server committed a protocol violation". This usually occurs because from .NET 1.1 SP1 onwards the parsing of HTTP responses became much more strict, as a security measure to prevent attacks which exploit malformed HTTP status lines and headers. The strict behaviour can be switched off via the application config file.
__________________
Sathish Kumar.R
Knowledge is meant to SHARE
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-10-2008, 02:38 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default Re: "System.Net.WebException: The server committed a protocol violation.

Hi Sathish..


Thanks. for your reply... Can you send me the code for web.config section..?


Thnks....
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-10-2008, 02:40 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Thumbs up Re: "System.Net.WebException: The server committed a protocol violation.

Hi ,
Here is the xml code section that you need

<?xml version ="1.0"?>
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>
__________________
Sathish Kumar.R
Knowledge is meant to SHARE
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-10-2008, 02:43 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default Re: "System.Net.WebException: The server committed a protocol violation.

Hi Sathish...

I found one more way to achive this...

From .NET 2.0 this behaviour can be configured programmatically using the HttpWebRequestElement useUnsafeHeaderParsing property. At first when I read about this I assumed it was a property that can be set dynamically at runtime, for example in the same way as the HttpWebRequest KeepAlive property. But in fact its used in the new 2.0 configuration infrastructure to set the value in the config file (although once you do this the new value applies to the current running application as well as any apps launched afterwards. The new configuration infrastructure is pretty complex but this code seems to work ok:


Code:
Configuration config = ConfigurationManager.OpenExeConfiguration(
  ConfigurationUserLevel.None);
SettingsSection section = (SettingsSection)config.GetSection(
  "system.net/settings");
section.HttpWebRequest.UseUnsafeHeaderParsing = false;
config.Save();

thnks..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-10-2008, 02:48 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Thumbs up Re: "System.Net.WebException: The server committed a protocol violation.

Hi,
Finally, I found the code below in a post on the .NET Framework Networking and Communication forum. This uses reflection to set the private field useUnsafeHeaderParsing to true and as a result may not be suitable in all scenarios where the relevant code access security permission is not available. (Note: add System.Configuration.dll as a reference to your project.)

Code:
public static bool SetAllowUnsafeHeaderParsing()
{
  //Get the assembly that contains the internal class
  Assembly aNetAssembly = Assembly.GetAssembly(
    typeof(System.Net.Configuration.SettingsSection));
  if (aNetAssembly != null)
  {
    //Use the assembly in order to get the internal type for 
    // the internal class
    Type aSettingsType = aNetAssembly.GetType(
      "System.Net.Configuration.SettingsSectionInternal");
    if (aSettingsType != null)
    {
      //Use the internal static property to get an instance 
      // of the internal settings class. If the static instance 
      // isn't created allready the property will create it for us.
      object anInstance = aSettingsType.InvokeMember("Section",
        BindingFlags.Static | BindingFlags.GetProperty 
        | BindingFlags.NonPublic, null, null, new object[] { });
      if (anInstance != null)
      {
        //Locate the private bool field that tells the 
        // framework is unsafe header parsing should be 
        // allowed or not
        FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField(
          "useUnsafeHeaderParsing", 
          BindingFlags.NonPublic | BindingFlags.Instance);
        if (aUseUnsafeHeaderParsing != null)
        {
          aUseUnsafeHeaderParsing.SetValue(anInstance, true);
          return true;
        }
      }
    }
  }
  return false;
}
__________________
Sathish Kumar.R
Knowledge is meant to SHARE
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between "using System.Data;" and directly adding the refer KiruthikaSambandam ASP and ASP.NET Programming 1 11-15-2007 01:33 AM
Anybody explain about "Common Type System" (CTS)? H2o ASP and ASP.NET Programming 5 08-09-2007 08:02 AM
What is the difference between "using System.Data;" and directly adding the reference H2o ASP and ASP.NET Programming 1 07-24-2007 03:33 AM
I get the error message "Unable to start debugging on the web server..." when I debug oxygen ASP and ASP.NET Programming 1 07-20-2007 04:50 AM
Don't Use "SP_" Prefix for Stored Procedure Name in SQL Server 200X Gopisoft Database Support 0 07-16-2007 04:01 AM


All times are GMT -7. The time now is 07:45 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0