HttpWebResponse“SeeOther”StatusCode返回而不是404

时间:2011-07-11 13:59:29

标签: asp.net httpwebrequest http-status-code-404 httpwebresponse

我正在做一个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;

1 个答案:

答案 0 :(得分:0)

不要使用OpenDNS?这是一个轻率的答案,但它是使用OpenDNS的缺点。他们永远不会返回找不到网络客户端检测404所需的主机。

虽然,如果您收到OpenDNS的回复,这意味着您的客户端找不到服务器。一旦你连接到它,404将来自服务器&它无法找到您请求的资源。

您需要先更改代码才能进行DNS查找 - 在System.Net中查找dnslookup类 - 如果查找失败则返回404。

现在实际发生的是:

  • 您的webclient在提供的主机名上执行dns查找
  • dns查找失败
  • OpenDNS拦截失败并返回其服务器的IP地址
  • 他们的服务器然后返回初始主机名的搜索响应以及延续代码。

您需要做的是以下场景

  • 您在主机名
  • 上执行dns查找
  • 主机不存在,因此您返回404

  • 您在主机名
  • 上执行dns查找
  • 主机存在,因此您提出请求
  • 主机找不到请求的路径,因此返回404
  • 你返回404

  • 您在主机名
  • 上执行dns查找
  • 主机存在,因此您提出请求
  • 路径存在,主机返回请求的数据。
  • 您将此信息发送给您的客户