【问题标题】:Is there a way of differentiating between Custom error pages and default error pages for webpages?有没有办法区分自定义错误页面和网页的默认错误页面?
【发布时间】:2012-09-07 16:24:45
【问题描述】:
我只是想知道是否有一种方法可以测试从服务器返回的 404 错误页面是否是该服务器的默认错误页面(我主要考虑从 IIS 返回的默认 404 页面)和已创建为特定于站点的自定义项?
当我向服务器发出请求时,返回 404 错误。有时标头中的内容长度为 0(通常由浏览器处理),否则返回错误页面。这意味着内容长度现在不再为 0,因此我无法轻松区分什么是自定义页面和什么只是普通的旧默认页面。
我考虑了一些内容分析方法,其中大部分都占用大量内存(尤其是对于较大的页面),我想知道是否有人找到了一个巧妙的技巧来比较这样的两个网页。
任何帮助/指针将不胜感激!
【问题讨论】:
标签:
c#
http-status-code-404
【解决方案1】:
只需比较返回页面的 HTML。很可能,您收到的页面是一个简单的字符串。只需将您收到的字符串与您存储在代码中的字符串进行比较,就像我在下面列出的代码一样。
IIS7默认404页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>404 - File or directory not found.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>404 - File or directory not found.</h2>
<h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>
</fieldset></div>
</div>
</body>
</html>