转载:https://www.cnblogs.com/wangyonglai/p/11327323.html
服务端代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace HttpListenerTest { public partial class FrmServer : Form { Thread watchThread = null; HttpListener httpListener = new HttpListener(); public FrmServer() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; } /// <summary> /// 启动监听 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStart_Click(object sender, EventArgs e) { //异步HttpListener监听 watchThread = new Thread(new ThreadStart(StartListen)); watchThread.Name = "监听线程"; watchThread.Start(); MessageBox.Show("开启成功"); lblUrl.Enabled = false; btnStart.Enabled = false; btnStop.Enabled = true; } /// <summary> /// 开启HttpListener监听 /// </summary> public void StartListen() { //String[] urls = { "http://localhost:5675/", "http://localhost:5676/" };//可以同时开启多个 String[] urls = { lblUrl.Text.ToString() };//可以同时开启多个 foreach (String url in urls) { httpListener.Prefixes.Add(url); } httpListener.Start(); //log.InfoFormat("Http服务器开始监听端口 http://localhost:5673/"); while (true) { HttpListenerContext context = httpListener.GetContext(); //阻塞,接收客户端请求 HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; //获取传递的body内容 using (System.IO.Stream body = request.InputStream) // here we have data { using (System.IO.StreamReader reader = new System.IO.StreamReader(body, request.ContentEncoding)) { string _body= reader.ReadToEnd(); lstMsg.Items.Insert(0, _body); //lstMsg.Items.Add(DateTime.Now.ToString("HH:mm:ss")); //lstMsg.TopIndex = lstMsg.Items.Count - 1; } } //返回响应正文数据 String responseString = @"<html><body><h1>Hellow,How are You?{DateTime.Now}</h1></body></html>"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); //关闭输出 output.Close(); } //log.InfoFormat("http服务器停止监听,输入任意键退出"); } /// <summary> /// 停止监听 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStop_Click(object sender, EventArgs e) { //服务停止监听 watchThread.Abort(); httpListener.Stop(); lblUrl.Enabled = true; btnStart.Enabled = true; btnStop.Enabled = false; } } }
客户端代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace HttpListenerClient { public partial class FrmClient : Form { public FrmClient() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; } private void btnSend_Click(object sender, EventArgs e) { VisitListen(); } /// <summary> /// 访问HttpListener,并获取响应 /// </summary> public void VisitListen() { //string httpUrl = "http://localhost:5675/"; string httpUrl = lblUrl.Text.ToString(); WebRequest webRequest = WebRequest.Create(httpUrl); webRequest.ContentType = "text/xml; charset=utf-8"; webRequest.Method = "POST"; string responseString = string.Format("<HTML><BODY> {0}</BODY></HTML>", txtSendMsg.Text.ToString()); lstMsg.Items.Insert(0, string.Format("发送信息:{0}" ,responseString)); //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); using (Stream requestStream = webRequest.GetRequestStream()) { byte[] paramBytes = Encoding.UTF8.GetBytes(responseString); requestStream.Write(paramBytes, 0, paramBytes.Length); } //响应 WebResponse webResponse = webRequest.GetResponse(); using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { string result = ""; result = myStreamReader.ReadToEnd(); lstMsg.Items.Insert(0, string.Format("获取到信息:{0}" ,result)); //log.InfoFormat("Http服务器获取到数据" + result); } } } }