HttpListener 服务器头 c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/427326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:57:01  来源:igfitidea点击:

HttpListener Server Header c#

c#httphttplistener

提问by Elijah Glover

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);
        }
    }

采纳答案by arul

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 。

回答by Elijah Glover

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

谢谢以利亚

回答by SolutionMaster5000

I know I'm a little late but I was just recently trying to do the same thing and I accidentally came across a solution that works but I'm unsure if it has any repercussions.

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

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