我正在为一个个人项目编写一个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,buffer.Length); output.Close(); Listener.BeginGetContext(ProcessRequest,Listener); } }
解决方法
@H_301_7@ HttpListener类封装了原生API HttpSendHttpResponse Function,其中链接中所述将始终将荒谬的文本附加到服务器头信息中.没有办法解决这个问题,除非你想从头编写你的HttpListener.