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>
You can use HttpWebRequest
’s AllowAutoRedirect
property:
…
var req = (HttpWebRequest)WebRequest.Create(URI);
req.AllowAutoRedirect = false;
…
来源:https://stackoverflow.com/questions/14012384/webrequest-prevent-redirection