473,480 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 6237
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1999
by: KK | last post by:
Hi Peoples, Can anyone give me any learning materials for learning C++. i.e. can you give me any docs or give me the link to any website that has info on programming in general or C++. (ok...
3
2304
by: Lodewijk van Haringhal | last post by:
I'am new with javascritping not with programming. Is there nobody who can help me with ths simple promblem? :) Please, please give me a hint. Please help me with this script. I have two lists in...
23
3230
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
1
1369
by: Steve | last post by:
Hi, I've asked this question a couple of times before on this forum but no one seems to be nice enough to point me to the right direction or help me out with any information, if possible. Please...
2
1870
by: tuan_vandyk | last post by:
Hi I desperately need help with my project. Theoretically everything should work bu it just isn't. Please email me for a copy of the project's source code. It was made in Turbo C++ 5. Please if...
13
2114
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
17
2264
by: Saps | last post by:
Hi all. Can anyone help me here. I have loads of .sql files and i need a way to call these from my asp page so the user can run them from the browser. Meaning i have a page with a list of all...
1
2272
by: glenn123 | last post by:
Hi, i am just about out of time to produce a working jukebox which has to perform these functions: to play music files when a track is chosen from a list which when the user presses the change genre...
2
2434
by: clouddragon | last post by:
Hi, i am in desperate need for any help regarding one of my assignments. I am to write a python program that lists the numbers that are composite from 1 to n(input) and write it to an external...
0
2716
by: uno7031 | last post by:
Help Please!!! Adding 5 Days to another Date in an access query Good Morning, Help please…. I am new to access and trying to write a query that will add 5 days between a RecDate and a...
0
7048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.