By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,795 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,795 IT Pros & Developers. It's quick & easy.

HttpWebRequest, Error 400 - Possible redirect?

P: n/a
Hello!

I'm trying to connect to different sites using an HttpWebRequest
object and some of them return an Error 400. If you navigate to it in
a normal browser, it works fine. This is an example of one:

http://www.nmprc.state.nm.us

Error: The remote server returned an error: (400) Bad Request.

I thought it might be because of a redirect that the HttpWebRequest
wasn't picking up and tried to code for that. This is what I have:

---------------------------- CODE --------------------------
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create( txtURL.Text );
request.AllowAutoRedirect = true;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
request.MaximumAutomaticRedirections = 5;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();
Encoding enc = Encoding.UTF8;
if ( response.ContentEncoding != string.Empty )
{
enc = Encoding.GetEncoding( response.ContentEncoding );
}

StreamReader reader = new StreamReader( responseStream, enc );
string StringToDisplay = reader.ReadToEnd();

txtOutput.Text = StringToDisplay;
---------------------------- CODE --------------------------

My test form has a textbox called "txtURL" where it gets the URL from.
It has a textbox ( txtOutput - multiline ) for displaying the text
results.
Drop these 2 controls on a form and a button to call the above code
and you can see what it's doing.

Do you think a redirect is causing the issue? If so, why isn't it
redirecting? If not, what else could be causing it? I think it might
be because the site realizes it's not a "real" web browser. If that's
it, how do I emulate a web browser? I tried to set the UserAgent, but
that didn't see to do anything.

TIA!
-BEP

May 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
BEP,

You could easily get a sniffer and see the requests that are being sent
by your browser and by your program, and then look at the differences.

I've used Fiddler in the past, but there are a number of them out there
which you could use.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<be*****@gmail.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
Hello!

I'm trying to connect to different sites using an HttpWebRequest
object and some of them return an Error 400. If you navigate to it in
a normal browser, it works fine. This is an example of one:

http://www.nmprc.state.nm.us

Error: The remote server returned an error: (400) Bad Request.

I thought it might be because of a redirect that the HttpWebRequest
wasn't picking up and tried to code for that. This is what I have:

---------------------------- CODE --------------------------
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create( txtURL.Text );
request.AllowAutoRedirect = true;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
request.MaximumAutomaticRedirections = 5;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();
Encoding enc = Encoding.UTF8;
if ( response.ContentEncoding != string.Empty )
{
enc = Encoding.GetEncoding( response.ContentEncoding );
}

StreamReader reader = new StreamReader( responseStream, enc );
string StringToDisplay = reader.ReadToEnd();

txtOutput.Text = StringToDisplay;
---------------------------- CODE --------------------------

My test form has a textbox called "txtURL" where it gets the URL from.
It has a textbox ( txtOutput - multiline ) for displaying the text
results.
Drop these 2 controls on a form and a button to call the above code
and you can see what it's doing.

Do you think a redirect is causing the issue? If so, why isn't it
redirecting? If not, what else could be causing it? I think it might
be because the site realizes it's not a "real" web browser. If that's
it, how do I emulate a web browser? I tried to set the UserAgent, but
that didn't see to do anything.

TIA!
-BEP

May 7 '07 #2

P: n/a
On May 7, 3:29 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
BEP,

You could easily get a sniffer and see the requests that are being sent
by your browser and by your program, and then look at the differences.
Got it, did it, and fixed it!

Thanks for the suggestion - it was exactly what I needed.

BEP

May 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.