Please help! HTTP protocol violation error using HttpWebRequest

G

Guest

Hello all:

I have a method that does a POST to a secured website using HttpWebRequest.
It worked when logging in the site, but it failed with an HTTP prococol
violation error when it is used to retrieve a report.

Error: The underlying connection was closed: The server committed an HTTP
protocol violation.

I have tried to the set useUnsafeHeaderParsing to true in the app config
file, but it still does not work.

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

Please tell me what I did wrong in the following code?

Thanks,

Tom

//-----------------------------CODE-----------------------------
private void WebRequestPost(string url, string postData)
{
try
{
byte[] buffer = Encoding.ASCII.GetBytes(postData);

mWebRequest = WebRequest.Create(url) as HttpWebRequest;
mWebRequest.Method = "POST";
mWebRequest.ContentType = "application/x-www-form-urlencoded";
mWebRequest.ContentLength = buffer.Length;
mWebRequest.CookieContainer = mCookies;
mWebRequest.AllowAutoRedirect = true;
mWebRequest.KeepAlive = false;
mWebRequest.ProtocolVersion = HttpVersion.Version10;

Stream reqstrm = mWebRequest.GetRequestStream();
reqstrm.Write(buffer, 0, buffer.Length);
reqstrm.Flush();
reqstrm.Close();

// Failed on this call.
HttpWebResponse res = (HttpWebResponse)mWebRequest.GetResponse();

Stream resst = res.GetResponseStream();
StreamReader sr = new StreamReader(resst);
Log(sr.ReadToEnd(), "postdata");
resst.Close();
sr.Close();
res.Close();
}
catch (Exception ex)
{
Program.TraceLog("Failed to post the form: " + ex.Message);
}
}
//----------------------------END CODE-------------------------------
 
P

PiotrKolodziej

Are you sure, that app.config file is created by your project application?
It want work if you just copy the file from other project, to given project
directory.
If you are using VS, its enought to click solution explorer and add new item
of config type.
 
P

PiotrKolodziej

Anyway it works for me with the following config file.

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.net>

<settings>

<httpWebRequest useUnsafeHeaderParsing = "true" />

</settings>

</system.net>

</configuration>
 
G

Guest

PiotrKolodziej,

Thanks for your response. Yes, I used the app config file for other settings
in my application too as followed:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="SpecialCharacter"
type="System.Configuration.NameValueSectionHandler" />
</configSections>

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

<system.diagnostics>
<switches>
<add name="MainTraceSwitch" value="4" />
</switches>
</system.diagnostics>

<appSettings>
<add key="ReportFileExtension" value="csv"/>
<add key="PathToReportConfigFile" value="C:\Test\Log"/>
</appSettings>

<SpecialCharacter>
<add key="/" value="%2F"/>
<add key=":" value="%3A"/>
</SpecialCharacter>
</configuration>

Thanks,

Tom
 
P

PiotrKolodziej

Hmm.
Code seems to be correct. At least mine's similar.
If you didn't handle the problem, please send me the project.
It appeared to be interesting for me, and i can still try to help you.

PK
 
J

Joerg Jooss

Thus wrote Tom,
Hello all:

I have a method that does a POST to a secured website using
HttpWebRequest. It worked when logging in the site, but it failed with
an HTTP prococol violation error when it is used to retrieve a report.

Error: The underlying connection was closed: The server committed an
HTTP protocol violation.

I have tried to the set useUnsafeHeaderParsing to true in the app
config file, but it still does not work.

<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
Please tell me what I did wrong in the following code?

Nothing -- it's a *server side* error.

I'm not sure whether unsafeHeaderParsing really allows all rubbish to be
accepted as HTTP. I'd rather try to get the server side fixed.

Cheers,
 
P

PiotrKolodziej

Nothing -- it's a *server side* error.
How can you say that's server side error. Did you see what server send?
Did you sniffed port for reply?
It's not an error. It's an exception. Since it's an exception it still can
work.

I'am advicing you Tom to sniff port, when server is sending a reply. ( It's
always good to know if everything works fine )
Then we could see what server replyes.
 
J

Joerg Jooss

Thus wrote PiotrKolodziej,
How can you say that's server side error. Did you see what server
send?

I can say that because that's what the exeptions *means*. And while the .NET
framework is using a construct called exception to notify a programmer of
that particular problem, it is an error for a server to send crap like \n\n
as EOL. From a HTTP perspective, such things must be considered to be an
error.
Did you sniffed port for reply?

No, I assumed that the .NET BCL is correct. Sniffing the HTTP traffic will
of course reveal the problem and is strongly recommended.
It's not an error. It's an exception. Since it's an exception it still
can work.

Reality check: If the server side commits a protocol violation, it's an error.
If there's no protocol violation, but the .NET framework throws an exception
for a perfectly valid HTTP response, it's an error as well (in the BCL though).


Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top