c# HttpWebRequest headers
Why the location is not listed on the response Headers?
My code:
string url = "http://hehe.freevar.com/files.php";
H开发者_JS百科ttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "HEAD";
Console.WriteLine(req.GetResponse().Headers);
From Wikipedia:
The HTTP Location header is returned in responses from an HTTP server under two circumstances:
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
I think you are looking for ResponseUri
property.
var responseUri = req.GetResponse().ResponseUri;
精彩评论