【发布时间】:2011-10-02 20:03:08
【问题描述】:
我正在执行一个应返回 404 状态代码的 Web 请求,因为该 URL 不存在,但它返回的是 303 (SeeOther) 状态。如果我查看它作为重定向 URL 返回的 URL,它是:
http://guide.opendns.com/?url=www.googuaoeuaoeu23p2le.com
有谁知道我可以如何防止这种情况并在没有 openDNS 重定向劫持的情况下查看原始 404?
我的代码:
// create the HttpWebRequest object
HttpWebRequest request = WebRequest.Create("www.googuaoeuaoeu23p2le.com") as HttpWebRequest;
// don't allow redirect
request.AllowAutoRedirect = false;
request.Method = "GET";
request.AllowWriteStreamBuffering = true;
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.SendChunked = false;
request.Credentials = CredentialCache.DefaultCredentials;
request.UserAgent = "NetMonitor";
// set the timeout
request.Timeout = 5000;
// get the data as an HttpWebResponse object
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
// convert the data into a string (assumes that we are requesting text)
StreamReader responseReader = new StreamReader(response.GetResponseStream());
// get HTML from the response
string responseHTML = responseReader.ReadToEnd();
// close the response reader
responseReader.Close();
// at this point we successfully got a response and just need to determine what type -- get status code
HttpStatusCode statusCode = response.StatusCode;
【问题讨论】:
标签: asp.net httpwebrequest http-status-code-404 httpwebresponse