删除HTTP标头“服务器”。在IIS 6中

时间:2012-07-16 05:22:10

标签: http-headers iis-6

我在IIS6中使用VS 2008。我想从Http Header“Server”中删除服务器标签。 我在Global.asax中使用了以下代码。

void Application_PreSendRequestHeaders(object src, EventArgs e)
{
        HttpContext.Current.Response.Headers.Remove("Server");
}

它显示错误“对象引用未设置为对象的实例”。 我该如何解决这个问题

2 个答案:

答案 0 :(得分:1)

以下是适用于我的步骤(适用于IIS6):

  1. 在网络服务器上下载并安装UrlScan 2.5
  2. 打开UrlScan配置文件(%windir%\ system32 \ inetsrv \ urlscan \ urlscan.ini)
  3. 找到RemoveServerHeader行并将值设置为1(应读取RemoveServerHeader = 1)
  4. 重置IIS并测试标头是否消失
  5. 仅供记录...如果您升级到IIS 7(集成管道模式),您可以使用自定义HttpModule代码实现此目的。

    祝你好运!

答案 1 :(得分:0)

在这里看一下答案:https://stackoverflow.com/a/12804722/2074016。它有额外的错误处理可以修复您的错误:

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
    // Remove the "Server" HTTP Header from response
    HttpApplication app = sender as HttpApplication;
    if (null != app && null != app.Request && !app.Request.IsLocal &&
        null != app.Context && null != app.Context.Response)
    {
        NameValueCollection headers = app.Context.Response.Headers;
        if (null != headers)
        {
            headers.Remove("Server");
        }
    }
}