Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRequest - Prevent redirection

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>
like image 915
Jaster Avatar asked Oct 27 '25 18:10

Jaster


1 Answers

You can use HttpWebRequest’s AllowAutoRedirect property:

…
var req = (HttpWebRequest)WebRequest.Create(URI);
req.AllowAutoRedirect = false;
…
like image 61
Ry- Avatar answered Oct 29 '25 06:10

Ry-



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!