在仅 HTTPS 的站点上正确响应 HTTP HEAD 请求
我们有一个只能通过 HTTPS 访问的 ASP.Net MVC3 站点,方法是使用控制器上的 RequireHTTPS 属性。
我们收到大量 HTTP HEAD 方法请求,主要来自 Twitter 机器人。默认的 ASP.Net/MVC3 响应是“500 内部服务器错误”,并且由 elmah 和 log4net 捕获/记录(现已过滤掉!)。
我可以根据这个问题编写一个特定的控制器和路由来处理这些非 HTTPS 请求 - 在 asp.NET MVC 3 中响应 HEAD 请求。
但是,从机器人的角度来看,最好的回应是什么? 200 表示服务器处于活动状态,302 重定向到 HTTPS url,还是坚持使用 500,因为该网站无法通过 HTTP 访问?
We have an ASP.Net MVC3 site only accessible over HTTPS, by using the RequireHTTPS attribute on the controller.
We are receiving numerous HTTP HEAD method requests, mainly from what appear to be Twitter bots. The default ASP.Net/MVC3 response is a '500 Internal Server Error', and are being caught/logged by elmah and log4net (now filtered out!).
I could write a specific controller and route to handle these non-HTTPS requests as per this question - Responding to HEAD Request in asp.NET MVC 3.
But, from the bots perspective what would be the best response? 200 to show the server is alive, a 302 redirect to the HTTPS url, or stick with the 500 as the site isn't accessible over HTTP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回应
你可以用以下方式
或用
什么手段
就我个人而言,我会选择
405
因为这是客户端的错误,“嘿伙计,我们这里不提供这些东西。” 似乎更适合我比“你到底在说什么?我听不懂。”之一,后者是由服务器无法识别请求方法位建议的501
描述。所有 HTTP 状态代码: http://www.w3.org/Protocols/ rfc2616/rfc2616-sec10.html
You could respond with
which means
or with
which means
Personally, I would go with the
405
since it's an error on the client side, a "Hey man, we don't serve that stuff here." seems more appropriate to me than "What the hell are you talking about? I don't understand it." one, the latter is suggested by the the server does not recognize the request method bit of the501
description.All the HTTP status codes: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
就我而言,我只在网站
/
的根目录上收到 HEAD 请求,这看起来像是机器人探测。所以,我有点担心返回 500 或 404。更多关于 405
根据 Albireo 的回答,405 可能没问题,但你需要返回接受的动词,例如:
302 选项
查看 MVC 代码中不重定向 HEAD 请求的注释:
似乎另一个选择是发送 302。返回 302 应该相当安全到 HTTPS 站点,以便 bot HEAD 请求根目录(这就是 MVC 对 GET 所做的操作)。因此,我根据 MVC 的方式实现了以下内容:
在 global.asax.cs 中实现:
In my case, I was only getting HEAD requests on the root of the site
/
which seems like bots probing. So, I was a bit worried about returning a 500 or 404.More on 405
405 may be OK as per Albireo's answer, but you need to return the accepted verbs, something like:
302 option
Looking at the comment in the MVC code which does not redirect the HEAD request:
It seems like another option is to send a 302. It should be reasonably safe to return a 302 to the HTTPS site for bot HEAD requests to root (which is what MVC does for a GET). So, I implemented the following which is based on the way that MVC does it:
Implement in global.asax.cs: