加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

HttpListener服务器头c#

发布时间:2020-12-15 04:13:48 所属栏目:百科 来源:网络整理
导读:我正在为一个个人项目编写一个C#http服务器,我想知道如何将返回的服务器头从Microsoft-HTTPAPI / 2.0更改为其他? public class HttpWebServer { private HttpListener Listener; public void Start() { Listener = new HttpListener(); Listener.Prefixes.A
我正在为一个个人项目编写一个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);
        }
    }

解决方法

HttpListener类封装了原生API HttpSendHttpResponse Function,其中链接中所述将始终将荒谬的文本附加到服务器头信息中.

没有办法解决这个问题,除非你想从头编写你的HttpListener.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读