473,385 Members | 1,400 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,385 software developers and data experts.

HttpWebRequest, Error 400 - Possible redirect?

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

Similar topics

2
by: Nick Jacobsen | last post by:
I am using HTTPWebRequest to connect to a server which requires authentication, then responds with a 302 redirect message, and redirects to DIFFERANT server, which also requires authentication (the...
2
by: Michael | last post by:
Hi all.. I need to submit an asynchronous request for a credit card authorization. I have an aspx page where the user confirms the transaction and clicks a button to send the transaction to the...
3
by: Hawksey | last post by:
I would like to be able to programatically(Using HTTPWebRequest and HTTPWebResponse) FORM post a user's credentials to a HTTPS login page (.ascx)control. The problem I think I have after attempting...
2
by: Maris Janis Vasilevskis | last post by:
Hi, Is it possible to force HttpWebRequest to do exactly (not approximately) the same as MSXML2.ServerXMLHTTP does? More details. I port JScript to JScript.NET I have a server (ASP invoking...
6
by: Oliver | last post by:
I have a very wired problem requesting one specific url from within my application. I have struggeled with this for 5 hours now, and searched google withour any luck, so i hope that someone are...
0
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the...
0
by: Alex Papadimoulis | last post by:
Hey Group, I'm in the process of converting an ASP-based site to an ASP.NET site and built a control that wraps around an ASP page. The control simply does a GET to the same server to render the...
5
by: Not4u | last post by:
Hello, Someone is using HttpWebRequest to automaticly post datas or retrieve datas from my site. How can i stop pages to be get by the HttpWebRequest method ? I know that pages get with...
0
by: barrybevel | last post by:
Hi, I'm trying to login to the www.vodafone.ie website using HttpWebRequest. It works fine with IE/Firefox and the .NET Web Control too, just not with my code. I think it's a redirect 302...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.