HttpListener Server Header c#
Asked Answered
G

4

11

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);
        }
    }
Grizzled answered 9/1, 2009 at 7:30 Comment(2)
IIS7 Is Much Faster, Multi-threaded GoodnessGrizzled
On the basic level, I suspect that HttpListener calls http.sys, thus the Http kernel queue should be the sameLeavitt
N
8

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.

Noman answered 9/1, 2009 at 15:10 Comment(0)
B
11

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");
Barcellona answered 19/4, 2015 at 19:41 Comment(5)
Yeah, this line by itself removes the Server header in response. But when after that I add another Server Header I get this: ,My Server! Microsoft-HTTPAPI/2.0Butterflies
TY! totally fixed it! I mean it would be cool to add my own, but /meh.Unread
Response.Headers.Add("Server", ""); Should also work fine.Murder
this weird hack pollutes the payloadVerniavernice
This doesn't work anymore. It just throws an expection. Or somethimes, if it doesn't, it will remove other headers, because they will happen to be after two \r\n's.Locoweed
N
8

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.

Noman answered 9/1, 2009 at 15:10 Comment(0)
G
1

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

Grizzled answered 9/1, 2009 at 7:53 Comment(0)
L
-2

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. 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. Its specifications are rather loose and it probably gets ignored by the majority of browsers.

Locoweed answered 19/7, 2021 at 2:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.