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