我正在做一个Web请求,该请求应返回404状态代码,因为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;
答案 0 :(得分:0)
不要使用OpenDNS?这是一个轻率的答案,但它是使用OpenDNS的缺点。他们永远不会返回找不到网络客户端检测404所需的主机。
虽然,如果您收到OpenDNS的回复,这意味着您的客户端找不到服务器。一旦你连接到它,404将来自服务器&它无法找到您请求的资源。
您需要先更改代码才能进行DNS查找 - 在System.Net中查找dnslookup类 - 如果查找失败则返回404。
现在实际发生的是:
您需要做的是以下场景
或
或