473,795 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Cre ate( txtURL.Text );
request.AllowAu toRedirect = true;
request.UserAge nt = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
request.Maximum AutomaticRedire ctions = 5;
HttpWebResponse response = (HttpWebRespons e)request.GetRe sponse();

Stream responseStream = response.GetRes ponseStream();
Encoding enc = Encoding.UTF8;
if ( response.Conten tEncoding != string.Empty )
{
enc = Encoding.GetEnc oding( response.Conten tEncoding );
}

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

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 4085
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.co m

<be*****@gmail. comwrote in message
news:11******** *************@y 80g2000hsf.goog legroups.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.Cre ate( txtURL.Text );
request.AllowAu toRedirect = true;
request.UserAge nt = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
request.Maximum AutomaticRedire ctions = 5;
HttpWebResponse response = (HttpWebRespons e)request.GetRe sponse();

Stream responseStream = response.GetRes ponseStream();
Encoding enc = Encoding.UTF8;
if ( response.Conten tEncoding != string.Empty )
{
enc = Encoding.GetEnc oding( response.Conten tEncoding );
}

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

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.guar d.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
1008
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 same credentials as the first server). My problem is I can get my client to authenticate to the first server, and redirect to the first server, but then when the second server responds with a 401 authentication required message, HTTPWebRequest...
2
5305
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 authorization provider. When the submit button is clicked, I hide the panel displaying the details and start a client-side progress bar using a literal control. This has to be an async call because if I start the request in the button click...
3
2795
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 this and after reading other posts is; that if the login is successful data is retrieved from DB telling the login process where(which page) this user will be redirected to(different company users will go to different pages). Is it possible to...
2
8758
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 binary code; code not accessible). I send XML requests to it. If my request is accepted, ServerXMLHTTP returns XML with accept details. If my request is rejected, ServerXMLHTTP returns XML with reject details.
6
1971
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 able to help me. Here is the code that i am using: Dim url As String = "MyIP" Dim httpRequest As HttpWebRequest = CType(WebRequest.Create(url),
0
1513
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 ASP content, and then just writes it to the page. In development and testing, there were no problems. Once deployed to production, after the site runs for a few hours, it starts to slow down and throw countless "Operation Has Timed Out"...
0
2039
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 ASP content, and then just writes it to the page. In development and testing, there were no problems. Once deployed to production, after the site runs for a few hours, it starts to slow down and throw countless "Operation Has Timed Out"...
5
1631
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 HttpWebRequest have is no referer. So for me the solution is to check the referer of the calling page, if the referer is my domain, ok i can send the page, if not i can redirect to an error page.
0
12798
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 problem. I'm using this code in a ASP.NET 2.0 application just in case that matters, maybe someone knows a better way to do this?
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10435
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.