简体   繁体   English

C#-httpwebrequest状态代码返回200而不是404

[英]c# - httpwebrequest status code returns 200 instead of 404

I'm trying this code.. 我正在尝试此代码。

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.goo4le.com/");
request.Method = "HEAD";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
  Console.Write((int)response.StatusCode);
}

goo4le is a non existing domain. goo4le是不存在的域。 So its supposed to return 404. Instead it returns 200 status. 因此它应该返回404。相反,它返回200状态。

I think its because of my broadband provider using a custom 404 page. 我认为这是因为我的宽带提供商使用了自定义404页面。

This is what i see when i enter goo4le.com in my browser. 这是我在浏览器中输入goo4le.com时看到的内容

Can someone tell me how to get the real http status instead of my browser status? 有人可以告诉我如何获取真实的http状态而不是我的浏览器状态吗?

I actually don't get any status code when running this, I get a DNS error saying I cant lookup the domain. 运行此程序时,我实际上没有任何状态代码,但出现DNS错误,提示我无法查找域。

I imagine you are exactly right about the ISP, they may be doing this via a DNS redirection given you dont get this error. 我想您对ISP完全正确,如果您没有收到此错误,他们可能通过DNS重定向来完成。 You could solve this by using a DNS server other than the one your ISP provides, try googles 8.8.8.8, 8.8.4.4 ( https://developers.google.com/speed/public-dns/ ) 您可以使用ISP提供的DNS服务器以外的其他DNS服务器来解决此问题,请尝试使用googles 8.8.8.8、8.8.4.4( https://developers.google.com/speed/public-dns/

This from their FAQs 这来自他们的常见问题解答

How is Google Public DNS different from my ISP's DNS service or other open DNS resolvers? Google Public DNS与我的ISP的DNS服务或其他开放DNS解析器有何不同? How can I tell if it is better? 我怎么知道是否更好?

Open resolvers and your ISP all offer DNS resolution services. 开放式解析器和您的ISP都提供DNS解析服务。 We invite you to try Google Public DNS as your primary or secondary DNS resolver along with any other alternate DNS services. 我们邀请您尝试使用Google Public DNS作为您的主要或辅助DNS解析器以及其他任何替代DNS服务。 There are many things to consider when identifying a DNS resolver that works for you, such as speed, reliability, security, and validity of responses. 确定适合您的DNS解析器时,需要考虑很多因素,例如速度,可靠性,安全性和响应的有效性。 Unlike Google Public DNS, some ISPs and open resolvers block, filter, or redirect DNS responses. 与Google Public DNS不同,某些ISP和开放式解析器会阻止,过滤或重定向DNS响应。

How does Google Public DNS handle non-existent domains? Google公共DNS如何处理不存在的域?

If you issue a query for a domain name that does not exist, Google Public DNS always returns an NXDOMAIN record, as per the DNS protocol standards. 如果您对不存在的域名进行查询,则根据DNS协议标准,Google Public DNS始终会返回NXDOMAIN记录。 The browser should show this response as a DNS error. 浏览器应将此响应显示为DNS错误。 If, instead, you receive any response other than an error message (for example, you are redirected to another page), this could be the result of the following: A client-side application such as a browser plug-in is displaying an alternate page for a non-existent domain. 相反,如果您收到除错误消息以外的任何响应(例如,您已重定向到另一个页面),则可能是以下原因导致的:客户端应用程序(例如浏览器插件)正在显示备用不存在的域的页面。 Some ISPs may intercept and replace all NXDOMAIN responses with responses that lead to their own servers. 某些ISP可能会拦截所有NXDOMAIN响应并将其替换为通往自己服务器的响应。 If you are concerned that your ISP is intercepting Google Public DNS requests or responses, you should contact your ISP. 如果您担心您的ISP拦截了Google Public DNS请求或响应,则应与您的ISP联系。

You can try to disable redirecting, unfortunatelly i can't test it, since i got another provider (if that's the problem). 您可以尝试禁用重定向,很遗憾,我无法测试它,因为我有另一个提供程序(如果那是问题)。

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.goo4le.com/");
request.Method = "HEAD";
request.AllowAutoRedirect = false;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
  Console.Write((int)response.StatusCode);
}

if it is working, it will most likely throw an exception, since everything that returns a statuscode that's not 200 throws one...... if you wanna catch it try this: 如果工作正常,则很可能会引发异常,因为返回状态码不是200的所有内容都会抛出一个异常……如果您想捕获它,请尝试以下操作:

try
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.goo4le.com/");
    request.Method = "HEAD";
    request.AllowAutoRedirect = false;
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
  Console.Write((int)response.StatusCode);
}

}
catch (WebException e)
{
   // in this case it was a status code exception (not status 200...)
   if (e.Response != null) Console.Write((int)e.Response.StatusCode);
   else throw;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果状态!= 200,则HttpWebRequest返回null - HttpWebRequest returns null if status != 200 从 HttpWebRequest 和 HttpWebResponse 获取 Http 状态码号(200、301、404等) - Getting Http Status code number (200, 301, 404, etc.) from HttpWebRequest and HttpWebResponse NotFound() 返回 200 而不是 404 - NotFound() returns 200 instead of 404 控制器操作错误地返回状态代码200而不是204 - Controller Action incorrectly returns status code 200 instead of 204 http状态代码200返回预期结果。 但是,当状态为BadRequest或未经授权时,我的C#webapi 1返回text / html - http status code 200 returns expected result. But when status is BadRequest or Unauthorized, my C# webapi 1 is returning text/html .NET Core TestServer 返回状态 200 而不是 404 以将请求发布到不存在的路径 - .NET Core TestServer returns status 200 instead of 404 for post request to non-existent path Web Api 返回 404 而不是 401 状态码 - Web Api returns 404 instead 401 status code C# HttpWebRequest 返回空白 - C# HttpWebRequest returns blank 当不存在数据时,返回具有状态代码 200 而不是 404 Not Found 错误的空数据集 - Return an empty data set with Status Code 200 instead of 404 Not Found error when no data is present Selenium C#-尽管网站可浏览,但HttpWebRequest始终返回404 - Selenium C# - HttpWebRequest always returns 404 despite sites being browse-able
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM