当前位置: 首页 > 编程语言 > C#

分享_11117

时间:2023-04-10 17:04:34 C#

使用HttpListener使用HttpListener我有下面的HTTP监听器方法,灵感来自MSDN的使用HttpListener类的例子。我对编程很陌生,我不知道从哪里开始从我的Main()初始化它。有什么建议么?publicstaticvoidHttpListener(string[]prefixes){if(prefixes==null||prefixes.Length==0)thrownewArgumentException("Prefixesneeded");HttpListener监听器=newHttpListener();foreach(stringsinprefixes){listener.Prefixes.Add(s);}listener.Start();Console.WriteLine("正在听..");HttpListenerContextcontext=listener.GetContext();HttpListenerRequestrequest=context.Request;HttpListenerResponse响应=上下文。回复;字符串responseString="测试";byte[]buffer=Encoding.UTF8.GetBytes(responseString);response.ContentLength64=buffer.Length;流输出=response.OutputStream;output.Write(buffer,0,buffer.Length);输出.关闭();听众停止();}你好像去掉了MSDNHttpListener类页面上提到的注释://URIprefixisrequired,eg"http://contoso.com:8080/index/"。所以就这样称呼它:publicstaticvoidMain(string[]args){HttpListener(new[]{"http://localhost/"});但请注意,此示例仅处理一个请求,然后退出。如果您的后续问题是“如何让它处理多个请求?”,请参阅使用C#HttpListener处理多个请求。你可以这样做:以上是C#学习教程:使用HttpListener分享的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——publicvoidListenTraces(){httpListener.Prefixes.Add(PORT_HOST);尝试{httpListener.Start();}catch(HttpListenerExceptionhlex){log.Warn("无法启动代理监听事务"+hlex);返回;}log.Info("现在准备好接收跟踪...");while(true){varcontext=httpListener.GetContext();//获取上下文log.Info("Newtraceconnexionincoming");Console.WriteLine(context.SomethingYouWant);}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: