Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HttpListener服务器头c#_C#_Http_Httplistener - Fatal编程技术网

HttpListener服务器头c#

HttpListener服务器头c#,c#,http,httplistener,C#,Http,Httplistener,我正试图为个人项目编写一个C#http服务器,我想知道如何将返回的服务器头从Microsoft HTTPAPI/2.0更改为其他内容 public class HttpWebServer { private HttpListener Listener; public void Start() { Listener = new HttpListener(); Listener.Prefixe

我正试图为个人项目编写一个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);
        }
    }
公共类HttpWebServer
{
私有HttpListener侦听器;
公开作废开始()
{
Listener=新的HttpListener();
Add(“http://*:5555/”;
Listener.Start();
BeginGetContext(ProcessRequest,Listener);
Console.WriteLine(“连接已启动”);
}
公共停车场()
{
Listener.Stop();
}
私有void ProcessRequest(IAsyncResult结果)
{
HttpListener=(HttpListener)result.AsyncState;
HttpListenerContext=listener.EndGetContext(结果);
字符串responseString=“Hello World”;
byte[]buffer=Encoding.UTF8.GetBytes(responseString);
context.Response.ContentLength64=buffer.Length;
System.IO.Stream输出=context.Response.OutputStream;
输出.写入(缓冲区,0,缓冲区.长度);
output.Close();
BeginGetContext(ProcessRequest,Listener);
}
}

我确实试过了,但它回来了 我的个人服务器Microsoft HTTPAPI/2.0

我还使用了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);
        }
private void ProcessRequest(IAsyncResult结果)
{
HttpListener=(HttpListener)result.AsyncState;
HttpListenerContext=listener.EndGetContext(结果);
字符串responseString=“Hello World”;
byte[]buffer=Encoding.UTF8.GetBytes(responseString);
context.Response.ContentLength64=buffer.Length;
//一个
AddHeader(“服务器”、“我的个人服务器”);
//两个
Remove(HttpResponseHeader.Server);
Add(HttpResponseHeader.Server,“我的个人服务器”);
//三
context.Response.Headers.Set(HttpResponseHeader.Server,“我的个人服务器”);
System.IO.Stream输出=context.Response.OutputStream;
输出.写入(缓冲区,0,缓冲区.长度);
output.Close();
BeginGetContext(ProcessRequest,Listener);
}
谢谢
Elijah

HttpListener类封装了本机API,如链接中所述,本机API总是将荒谬的文本附加到服务器头信息中


除非你想从头开始编写你的HttpListener,否则没有办法解决这个问题。

我知道我有点晚了,但我最近也在尝试做同样的事情,我意外地发现了一个有效的解决方案,但我不确定它是否有任何影响

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

IIS7速度更快,多线程性能在基本层面上,我怀疑
HttpListener
调用
http.sys
,因此http内核队列应该是sameYeah,这一行本身会删除响应中的服务器头。但是,当我添加另一个服务器头之后,我得到了这样的消息:,我的服务器!微软HTTPAPI/2.0TY!完全修好了!我的意思是,添加我自己的,但是/meh.
Response.Headers.add(“服务器”),会很酷
也可以正常工作。这种奇怪的黑客行为会污染有效负载