本文介绍了的HttpListener服务器头C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图写一个个人项目C#http服务器,我想知道我怎么可以改变从微软HTTPAPI / 2.0,到别的东西?
$ B $返回服务器头b
公共类HttpWebServer
{
私人的HttpListener监听;
公共无效启动()
{
=听众新的HttpListener();
Listener.Prefixes.Add(HTTP:// *:5555 /);
Listener.Start();
Listener.BeginGetContext(的ProcessRequest,监听器);
Console.WriteLine(连接开始);
}
公共无效停止()
{
Listener.Stop();
}
私人无效的ProcessRequest(IAsyncResult的结果)
{
的HttpListener监听=(的HttpListener)result.AsyncState;
HttpListenerContext背景= listener.EndGetContext(结果);
串responseString =< HTML和GT;的Hello World< / HTML>中;
字节[]缓冲= Encoding.UTF8.GetBytes(responseString);
context.Response.ContentLength64 = buffer.Length;
的System.IO.Stream输出= context.Response.OutputStream;
output.Write(缓冲液,0,buffer.Length);
output.Close();
Listener.BeginGetContext(的ProcessRequest,监听器);
}
}
解决方案
的的HttpListener类封装了底层API,,其中在规定的链接将始终追加荒谬文本到服务器头信息。
有没有办法如何解决这个问题,除非你想从头开始编写你的HttpListener。
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);
}
}
解决方案
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.
这篇关于的HttpListener服务器头C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!