473,396 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 6229
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
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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.