Reputation: 8581
I am using a WebRequest
to read an HTML site. The server seems to be redirecting my request.
My Code is similar to the following:
String URI = "http://www.foo.com/bar/index.html"
WebRequest req = WebRequest.Create(URI);
WebResponse resp = req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
String returnedContent = sr.ReadToEnd();
When I check the content of the returnedContent
it contains the content from a redirection like "http://www.foo.com/FOO_BAR/index.html". I am sure my requested URL exists since it is part of the recieved response (as an IFrame).
Is there a way to prevent the WebResponse
to be redirected and get the content of the requested url?
UPDATE
Setting req.AllowAutoRedirect = false
leads to a 302 Found
state code, but does not deliver the acutal content.
Some more details:
My requested url was http://www.foo.com/bar/index.html
the content I receive is located in http://www.foo.com/FOO_BAR/index.html
The response looks similar to this:
<body>
<div>
<iframe src="/foo/index.html"></iframe>
</div>
</body>
Upvotes: 4
Views: 9584
Reputation: 409
I know this is a rather old post, but thought I'd share what I found in my particular situation...
I was trying to access a site to see if specific content is available. I found that it kept redirecting away from the site I requested to a local version of the site. I noticed some cookies that were responsible for the redirect. If anyone else runs into the same issue, it's possible that there's a cookie or some other request header that you can play with to prevent the redirect away from your intended target.
In my case all I had to do was something like this:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//whatever other headers you need...
request.Headers.Add(HttpRequestHeader.Cookie, "ipRedirectOverride=true");
Note that this is just an example, your cookie name / value may be different
Upvotes: 0
Reputation: 224903
You can use HttpWebRequest
’s AllowAutoRedirect
property:
…
var req = (HttpWebRequest)WebRequest.Create(URI);
req.AllowAutoRedirect = false;
…
Upvotes: 13