Connecting Tech Pros Worldwide Help | Site Map

Protocol Violation

Kevinyy's Avatar
Member
 
Join Date: Jul 2008
Posts: 77
#1: Sep 5 '08
Here is my code snippet:
Expand|Select|Wrap|Line Numbers
  1. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
  2. request.KeepAlive = true;
  3. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  4.  //The GetResponse is where the error occurs
  5. Stream responsestream = response.GetResponseStream();
  6.  
Error:The server committed a protocol violation. Section=ResponseStatusLine


I can get the response from certain sites but then some cause my program to throw that error.
Is there anything i can do?
Kevinyy's Avatar
Member
 
Join Date: Jul 2008
Posts: 77
#2: Sep 5 '08

re: Protocol Violation


Ive checked other resources and i still haven't had any luck :(
joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#3: Sep 5 '08

re: Protocol Violation


could it be the .keepalive method ? try it without, because you said some site's right? or like in a try catch statement, set the .keepalive to false and redo the method maybe...

joedeene
Kevinyy's Avatar
Member
 
Join Date: Jul 2008
Posts: 77
#4: Sep 5 '08

re: Protocol Violation


ive already tried that, still same outcome :(
joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#5: Sep 5 '08

re: Protocol Violation


Quote:

Originally Posted by Kevinyy

ive already tried that, still same outcome :(

*THE FOLLOWING IS A POST FROM A DIFFERENT USER ON A DIFFERENT FORUM TO SEE THE LINK PLEASE PM ME:

Quote:

Originally Posted by Steven Cheng(OTHER SITE)

From your description, you're using the HttpWebRequest component to send
some http request to some external web resource in your ASP.NET web
application. However, you're always getting the "The server committed a
protocol violation. Section=ResponseStatusLine" error unless you set the
following section in the web.config file:

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

and you're wondering the cause of this behavior ,correct?

As for this issue, I've performed some research on this and found that the
problem is actually caused by the critical http header parsing/validating
of the HttpWebRequest component. According to the Http
Specification(http1.1), the HTTP header keys shoud specifically not include
any spaces in their names. However, some web servers do not fully respect
standards they're meant to. Applications running on the Dotnet framework
and making heavy use of http requests usually use the httpWebRequest class,
which encapsulates everything a web oriented developer could dream of. With
all the recently issues related to security, the "httpWebRequest" class
provides a self protection mechanism preventing it to accept HTTP answers
which not fully qualify to the specifications.

The common case is having a space in the "content-length" header key. The
server actually returns a "content length" key, which, assuming no spaces
are allowed, is considered as an attack vector (HTTP response split
attack), thus, triggering a "HTTP protocol violation error" exception.


And it is possible since the 1.1 SP 1 DotNet version to disable this error
check. Fortunately, DotNet allows you to modify some parameters directly
through a simple text configuration file . and that's just the setting
you mentioned ealier in your message.

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

Hope this helps clarify this problem some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

so it sounds similar to your problem, if your using asp.net, if not try typing the error code in google...

joedeene

**i take no credit for the work of "Steven Cheng"
Kevinyy's Avatar
Member
 
Join Date: Jul 2008
Posts: 77
#6: Sep 5 '08

re: Protocol Violation


Quote:

Originally Posted by joedeene

*THE FOLLOWING IS A POST FROM A DIFFERENT USER ON A DIFFERENT FORUM TO SEE THE LINK PLEASE PM ME:



so it sounds similar to your problem, if your using asp.net, if not try typing the error code in google...

joedeene

**i take no credit for the work of "Steven Cheng"

how do i add this modification? (useUnsafeHeaderParsing="true|false")
C#.Net2.2
joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#7: Sep 5 '08

re: Protocol Violation


Quote:

Originally Posted by Kevinyy

how do i add this modification? (useUnsafeHeaderParsing="true|false")
C#.Net2.2

well if your using asp.net that goes into a config file right? or are you just doing a windows app?

joedeene
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,158
#8: Sep 5 '08

re: Protocol Violation


According to HTTP protocol, all responses to a web request should start something like this:
"HTTP/1.1 200 OK"+(newline)

The error occurs when, for some servers, when serving up a certain type of file (varriable, depending on how the server software was built, or software age), that line is left out. I had the same errors in a program I wrote, because I forgot to include that line for some special AJAX pages.

As to how to fix it, looks like you found that. The statement looks like it would go somewhere in either the web.config file.
Kevinyy's Avatar
Member
 
Join Date: Jul 2008
Posts: 77
#9: Sep 5 '08

re: Protocol Violation


Quote:

Originally Posted by Plater

According to HTTP protocol, all responses to a web request should start something like this:
"HTTP/1.1 200 OK"+(newline)

The error occurs when, for some servers, when serving up a certain type of file (varriable, depending on how the server software was built, or software age), that line is left out. I had the same errors in a program I wrote, because I forgot to include that line for some special AJAX pages.

As to how to fix it, looks like you found that. The statement looks like it would go somewhere in either the web.config file.

Im writing a windows app. and i dont have a web.config file to amend, what should i do?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,158
#10: Sep 5 '08

re: Protocol Violation


Quote:

Originally Posted by Kevinyy

Im writing a windows app. and i dont have a web.config file to amend, what should i do?

Well I honesty am not sure where you would put this code, somewhere that happens at the begining of the program I would think
Expand|Select|Wrap|Line Numbers
  1. System.Net.Configuration.HttpWebRequestElement hwre = new System.Net.Configuration.HttpWebRequestElement();
  2. hwre.UseUnsafeHeaderParsing = true;
  3.  
Kevinyy's Avatar
Member
 
Join Date: Jul 2008
Posts: 77
#11: Sep 5 '08

re: Protocol Violation


Quote:

Originally Posted by Plater

Well I honesty am not sure where you would put this code, somewhere that happens at the begining of the program I would think

Expand|Select|Wrap|Line Numbers
  1. System.Net.Configuration.HttpWebRequestElement hwre = new System.Net.Configuration.HttpWebRequestElement();
  2. hwre.UseUnsafeHeaderParsing = true;
  3.  

ahhhh ok ill try that out, thank you!
Kevinyy's Avatar
Member
 
Join Date: Jul 2008
Posts: 77
#12: Sep 5 '08

re: Protocol Violation


no prevail! It still happens..gah this is soo fustrating :(
...and why does youtube.com throw a 400 error?
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#13: Sep 5 '08

re: Protocol Violation


Quote:

Originally Posted by Kevinyy

no prevail! It still happens..gah this is soo fustrating :(
...and why does youtube.com throw a 400 error?

A 400 Error is a bad request...
Kevinyy's Avatar
Member
 
Join Date: Jul 2008
Posts: 77
#14: Sep 6 '08

re: Protocol Violation


Quote:

Originally Posted by insertAlias

A 400 Error is a bad request...

i know...but youtube.com is a fine request - so i dont see whats going on there.


and not including the youtube thing, the program still errors with the Protocol Violation.. :(
Kevinyy's Avatar
Member
 
Join Date: Jul 2008
Posts: 77
#15: Sep 6 '08

re: Protocol Violation


Quote:

Originally Posted by Plater

Well I honesty am not sure where you would put this code, somewhere that happens at the begining of the program I would think

Expand|Select|Wrap|Line Numbers
  1. System.Net.Configuration.HttpWebRequestElement hwre = new System.Net.Configuration.HttpWebRequestElement();
  2. hwre.UseUnsafeHeaderParsing = true;
  3.  

Is there a certain way to use this? i added the reference to System.Net.Configuration, and i placed that code in the beginning of the program, would i have to change my program more to make use of that configuration?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,158
#16: Sep 8 '08

re: Protocol Violation


I guess you could try putting it RIGHT before any of your httpwebrequest stuff?
joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#17: Sep 8 '08

re: Protocol Violation


Quote:

Originally Posted by Kevinyy

Is there a certain way to use this? i added the reference to System.Net.Configuration, and i placed that code in the beginning of the program, would i have to change my program more to make use of that configuration?

Perhaps take a look at this LINK

joedeene
Reply