如何在Internet Explorer中禁止"友好错误消息"?

Ian*_*oyd 24 internet-explorer

我想显示一个自定义错误页面:

<!doctype html>
<html>
<head><title>400 Bad Request</title></head>
<body><h1>400 Bad Request</h1>
The grob must be in the frobber.
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

不幸的是,Internet Explorer忽略了HTTP服务器发送的响应; 隐藏我的页面并展示自己的:

通用

如何说服Internet Explorer显示用户发送的页面?

Ian*_*oyd 37

解决方案是PADDING.

Microsoft在知识库文章KB294807中注释:

HOW TO:关闭服务器端的Internet Explorer 5.x和6.x"显示友好HTTP错误消息"功能

...仅当发送到客户端的响应小于或等于指定阈值时,才会显示这些"友好"错误消息.例如,要查看HTTP 500响应的确切文本,内容长度必须大于512字节.

实现此填充.为此,请使用VBScript String函数返回相同字符的字符串,该字符串比Internet Explorer 5.x用于显示友好错误消息的ErrorThreshold多一个.例如,在500-100.asp标记之前添加以下行:

 <% Response.Write String(513, "_") %>
Run Code Online (Sandbox Code Playgroud)

把它做大

所以我将响应页面增加到:

<!doctype html>
<html>
<head><title>400 Bad Request</title></head>
<body><h1>400 Bad Request</h1>
The grob must be in the frobber.

<!--       
    512 bytes of padding to suppress Internet Explorer's "Friendly error messages"

    From: HOW TO: Turn Off the Internet Explorer 5.x and 6.x "Show Friendly HTTP Error Messages" Feature on the Server Side
          http://support.microsoft.com/kb/294807

    Several frequently-seen status codes have "friendly" error messages 
    that Internet Explorer 5.x displays and that effectively mask the 
    actual text message that the server sends.
    However, these \"friendly\" error messages are only displayed if the 
    response that is sent to the client is less than or equal to a 
    specified threshold.
    For example, to see the exact text of an HTTP 500 response, 
    the content length must be greater than 512 bytes.
  -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

问题解决了.

奖金阅读

是什么让IE决定显示一个友好的错误页面?

答案是服务器的响应必须符合两个标准:

  • HTTP状态代码必须是[400,403,404,405,406,408,409,410,500,501,505]
  • HTTP响应主体的字节长度必须短于阈值

字节长度阈值存储在子项\ SOFTWARE\Microsoft\Internet Explorer\Main\ErrorThresholds下的HKEY_LOCAL_MACHINE中的注册表中.

  • [403,405,410]: 256字节
  • [400,404,406,408,409,500,501,505]: 512字节
  • 否则:512字节

  • @andrew原因是因为大多数Web服务器显示非常无用的"404未找到在此服务器上找不到请求的资源".IE尝试捕获这样一个无用的错误消息,并显示对用户有用的东西.***如果***响应超过512字节,可以说网站本身正在尝试显示一条有用的消息来使用用户:所以它将使用它.我当然想不出除了长度之外更容易捕捉到无意义的"404"信息. (15认同)
  • 人们讨厌IE的又一个原因. (14认同)
  • 值得注意的是相应的MSDN博客的最后一句话:"显然,如果你要阻止友好错误页面的显示,强烈建议你提供一个比默认错误页面更友好的错误页面;只需禁用友好错误没有提供更好的信息的消息不是很有礼貌." (3认同)
  • 为什么打扰这个条件的内容长度呢?叹 (2认同)
  • @RocketHazmat 讽刺的是 Chrome 也开始做同样的事情。 (2认同)