简体   繁体   English

HttpListener 服务器头 c#

[英]HttpListener Server Header c#

I am trying to write a C# http server for a personal project, i am wondering how i can change the returned server header from Microsoft-HTTPAPI/2.0, to something else?我正在尝试为个人项目编写 C# http 服务器,我想知道如何将返回的服务器标头从 Microsoft-HTTPAPI/2.0 更改为其他内容?

 public class HttpWebServer
    {
        private HttpListener Listener;

        public void Start()
        {
            Listener = new HttpListener();
            Listener.Prefixes.Add("http://*:5555/");
            Listener.Start();
            Listener.BeginGetContext(ProcessRequest, Listener);
            Console.WriteLine("Connection Started");
        }

        public void Stop()
        {
            Listener.Stop();
        }

        private void ProcessRequest(IAsyncResult result)
        {
            HttpListener listener = (HttpListener)result.AsyncState;
            HttpListenerContext context = listener.EndGetContext(result);

            string responseString = "<html>Hello World</html>";
            byte[] buffer = Encoding.UTF8.GetBytes(responseString);

            context.Response.ContentLength64 = buffer.Length;
            System.IO.Stream output = context.Response.OutputStream;
            output.Write(buffer, 0, buffer.Length);
            output.Close();

            Listener.BeginGetContext(ProcessRequest, Listener);
        }
    }

The HttpListener class encapsulates the native API, HttpSendHttpResponse Function , which as stated in the link will always append the preposterous text to the server header information. HttpListener 类封装了本机 API HttpSendHttpResponse Function ,如链接中所述,它将始终将荒谬的文本附加到服务器标头信息中。

There's no way how to fix that, unless you want to code your HttpListener from scratch.没有办法解决这个问题,除非你想从头开始编写你的 HttpListener 。

我知道我有点晚了,但我最近刚尝试做同样的事情,我不小心遇到了一个有效的解决方案,但我不确定它是否有任何影响。

Response.Headers.Add("Server", "\r\n\r\n");

I did try, but it comes back with My Personal Server Microsoft-HTTPAPI/2.0我确实尝试过,但它返回了我的个人服务器 Microsoft-HTTPAPI/2.0

I have also used with no success, set, remove, add, addheader我也用过,没有成功,set,remove,add,addheader

private void ProcessRequest(IAsyncResult result)
        {
            HttpListener listener = (HttpListener)result.AsyncState;
            HttpListenerContext context = listener.EndGetContext(result);

            string responseString = "<html>Hello World</html>";
            byte[] buffer = Encoding.UTF8.GetBytes(responseString);
            context.Response.ContentLength64 = buffer.Length;

            //One
            context.Response.AddHeader("Server", "My Personal Server");

            //Two
            context.Response.Headers.Remove(HttpResponseHeader.Server);
            context.Response.Headers.Add(HttpResponseHeader.Server, "My Personal Server");

            //Three
            context.Response.Headers.Set(HttpResponseHeader.Server, "My Personal Server");

            System.IO.Stream output = context.Response.OutputStream;
            output.Write(buffer, 0, buffer.Length);
            output.Close();

            Listener.BeginGetContext(ProcessRequest, Listener);
        }

Thanks Elijah谢谢以利亚

Arul's answer is correct, however, as Iain Ballard already noted, setting the Server header to an empty string will work, meaning the Server header will not be sent at all. Arul 的回答是正确的,但是,正如 Iain Ballard 已经指出的那样,将 Server 标头设置为空字符串将起作用,这意味着根本不会发送 Server 标头。 Then, if you have to let the client know what your awesome app's name is, you can take the liberty of adding a "Via" header.然后,如果您必须让客户知道您的出色应用程序的名称是什么,您可以随意添加“Via”标题。 Its specifications are rather loose and it probably gets ignored by the majority of browsers.它的规范相当松散,可能会被大多数浏览器忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM