views:

403

answers:

2

Hi

In .Net 2.0 how can I disallow AutoRedirect when using WebRequest? I found some source code were there where used an AllowAutoRedirect property, but on my WebRequest there is no such property.

+5  A: 

You need to cast it to a HttpWebRequest

HttpWebRequest WebRequest =
(HttpWebRequest)System.Net.WebRequest.Create("http://www.mySite.com");
WebRequest.AllowAutoRedirect = false;
cgreeno
I'm glad you undeleted this - I was wondering why, given that it shows the cast :)
Jon Skeet
I made an error while editing...
cgreeno
+1  A: 

WebRequest itself doesn't have such a property, but HttpWebRequest does. If you cast to HttpWebRequest, you can set AllowAutoRedirect to false.

If your web request really isn't an HttpWebRequest, please explain what kind of request it is.

Jon Skeet
I've noticed that most people use the term "WebRequest" and "HttpWebRequest" almost synonymously. Do you think it's because the FileWebRequest class isn't commonly used?
Cerebrus
@Cerebrus: I've certainly never used it.
Jon Skeet