HttpWebRequest, Error 400 - Possible redirect?

B

bepnewt

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
 
N

Nicholas Paldino [.NET/C# MVP]

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

bepnewt

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
 

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