471,325 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Please help! HTTP protocol violation error using HttpWebRequest

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?

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-------------------------------

Feb 9 '06 #1
7 6085
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.


Feb 10 '06 #2
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>
Feb 10 '06 #3
Tom
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

"PiotrKolodziej" wrote:
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.


Feb 10 '06 #4
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
Feb 10 '06 #5
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,
--
Joerg Jooss
ne********@joergjooss.de
Feb 13 '06 #6
> 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.

Feb 13 '06 #7
Thus wrote PiotrKolodziej,
Nothing -- it's a *server side* error.
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,
--
Joerg Jooss
ne********@joergjooss.de
Feb 13 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Lodewijk van Haringhal | last post: by
23 posts views Thread by Jason | last post: by
1 post views Thread by Steve | last post: by
2 posts views Thread by tuan_vandyk | last post: by
17 posts views Thread by Saps | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.