HttpListener Server Header c#

狂风中的少年 提交于 2019-12-30 04:27:25

问题


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?

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

回答1:


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.

There's no way how to fix that, unless you want to code your HttpListener from scratch.




回答2:


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



回答3:


I did try, but it comes back with My Personal Server Microsoft-HTTPAPI/2.0

I have also used with no success, 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



来源:https://stackoverflow.com/questions/427326/httplistener-server-header-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!