HttpListener通讯成功案例

网友投稿 787 2022-11-24

HttpListener通讯成功案例

HttpListener通讯成功案例

1、创建WindowsService,如下代码

using System;using System.Net;using System.Net.Sockets;using System.ServiceProcess;using System.Text;using System.Threading;

namespace WindowsService{ public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); }

public static int TerminalPort = 12800; public static Thread ListeningThread; public static int Port; public static Encoding Encoding;

protected override void OnStart(string[] args) { InitialAcceptor(TerminalPort); }

protected override void OnStop() { }

public void DebugTest() { Console.WriteLine("StartService..."); InitialAcceptor(TerminalPort); Console.WriteLine("Terminal Listened on Port=" + TerminalPort); }

public void InitialAcceptor(int tunnelPort) { Port = tunnelPort;

Encoding = Encoding.UTF8;

ListeningThread = new Thread(new ThreadStart(StartListening));

ListeningThread.IsBackground = Environment.OSVersion.Platform != PlatformID.Unix;// ListeningThread.Start();

}

#region MyRegion //internal void StartListening() //{ // try // { // while (true) // { // try // { // listener = new HttpListener(); // listener.Prefixes.Add("+ Port + "/"); // listener.Start(); // } // catch // { // Thread.Sleep(1000); // }

// try // { // while (true) // { // IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); // result.AsyncWaitHandle.WaitOne(); // } // } // catch (Exception e) // { // } // } // } // catch // { // } //} #endregion internal void StartListening() { try { while (true) { try { #region MyRegion //服务端: //var listener = new HttpListener(); //listener.Prefixes.Add(" //listener.Start();

HttpListener listener = new HttpListener(); listener.Prefixes.Add("+ 1234 + "/"); listener.Start();

while (true) { IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); result.AsyncWaitHandle.WaitOne(); } #endregion } catch { Thread.Sleep(1000); } } } catch { } } private void ListenerCallback(IAsyncResult result) { HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); processRequest(context);

}

private void processRequest(HttpListenerContext context) { HttpListenerRequest request = context.Request; try { //string url = System.Web.HttpUtility.UrlDecode(request.RawUrl); string ss = request.QueryString["eid"]; context.Response.ContentEncoding = UTF8Encoding.UTF8; context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); //context.Response.ContentType = "image/jpg"; //context.Response.ContentLength64 = buffer.Length; var output = context.Response.OutputStream; byte[] nn = System.Text.Encoding.Default.GetBytes("qqqqqqqqqqqq"); output.Write(nn, 0, nn.Length); output.Close(); } catch (Exception e) {

} finally { } }

}}

2、创建控制台,测试启动WindowsService服务

using System;using System.Collections.Generic;using System.Text;using System.Threading;using WindowsService;

namespace WindowsService1TestConsole{ class Program { static void Main(string[] args) { try { Service1 service1 = new Service1();#if DEBUG service1.DebugTest();#endif Thread.CurrentThread.IsBackground = false; Thread.Sleep(Timeout.Infinite); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.Source); Console.WriteLine(e.StackTrace); Console.WriteLine(e.InnerException != null ? e.InnerException.ToString() : ""); Console.ReadLine(); } } }}

3、测试地址栏输入:request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8";

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd();//返回结果 myStreamReader.Close(); myResponseStream.Close();

龙腾一族至尊龙骑

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:socket-WebSocket-HttpListener-TcpListener服务端客户端的具体使用案例
下一篇:Windows服务启动进程----Cjwdev.WindowsApi.dll
相关文章

 发表评论

暂时没有评论,来抢沙发吧~