1

我遇到了 Firefox 的问题。我使用 HttpListener 实现了一个 Web 服务。工作得很好,我现在遇到的唯一问题是,firefox 似乎没有正确解释我的 404。

我对 HttpResponse 对象所做的只是将 StatusCode 设置为 404 并关闭它。没有其他的。

Internetexplorer 正确显示标准的 404 页面,Firefox 显示一个空页面,或者如果 url 以 xml 结尾,例如,它会给出一个 XML-Parsing 错误。

我究竟做错了什么?

这是实际上并没有做太多的代码,但这可能是问题所在,我不知道。

void handlePageNotFound(HttpListenerResponse response)
{
    response.StatusCode = 404;
    response.Close();
}

我为 Firefox 安装了一个插件来检查状态码是否被正确接收。这是。

4

1 回答 1

1

通常,Web 服务器会保留单独的 html 文件以在找不到页面时显示,例如 404.html。因此,除了发送此消息之外,我认为 Mozilla 会等待 Web 服务器提供适当的内容,而不是显示默认页面。

所以我在 response.StatusCode = 404 中添加了额外的行

try
{
    context.Response.ContentType = "text/html";
    string str = "<center>404 - Page not found</center>";
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);                    
    context.Response.OutputStream.Write(bytes, 0, bytes.Length);
    context.Response.OutputStream.Flush();
    context.Response.StatusCode = (int)((e is FileNotFoundException || e is DirectoryNotFoundException) ? HttpStatusCode.NotFound : HttpStatusCode.InternalServerError);
    context.Response.StatusDescription = e.Message;
}
catch
{
    Logger.LogError("Exception processing request 'ProcessFileRequest' - Catch block: {0}", e);
}

更好的方法可能是拥有 404.html 文件并为这种情况提供其内容。

于 2013-12-28T03:57:05.553 回答