0

Why the location is not listed on the response Headers?

My code:

        string url = "http://hehe.freevar.com/files.php";
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
        req.Method = "HEAD";
        Console.WriteLine(req.GetResponse().Headers);

2 Answers 2

4

From Wikipedia:

The HTTP Location header is returned in responses from an HTTP server under two circumstances:

  1. To force a web browser to load a different web page. It is passed as part of the response by a web server when the requested URI has:

    • Moved temporarily, or
    • Moved permanently

The HttpWebRequest class has a property AllowAutoRedirect which defaults to true:

Set AllowAutoRedirect to true if you want the request to automatically follow HTTP redirection headers to the new location of the resource.

This means you will never see the redirect request unless you set AllowAutoRedirect to false before making the request:

string url = "http://hehe.freevar.com/files.php";
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.AllowAutoRedirect = false;
req.Method = "HEAD";
Console.WriteLine(req.GetResponse().Headers);

Then you get the following response which does include Location:

Keep-Alive: timeout=1, max=10000
Connection: Keep-Alive
Content-Type: text/html
Date: Wed, 01 Jun 2011 01:32:18 GMT
Location: http://www.160by2.com/post-registration.aspx
Server: Apache
X-Powered-By: PHP/5.2.13
Sign up to request clarification or add additional context in comments.

Comments

2

I think you are looking for ResponseUri property.

var responseUri = req.GetResponse().ResponseUri;

1 Comment

+1 depending on the scenario this might be what OP was actually looking for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.