c#使用HttpListener监听post及get请求方法

c#使用HttpListener监听post及get请求方法示范

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
using System.Net;
using System.IO;
 
namespace testHttpListener
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        public HttpListener listener;
 
        private void Form1_Load(object sender, EventArgs e)
        {
            startHttpListen();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
 
 
        public void startHttpListen()
        {
            listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:8888/"); //添加需要监听的url范围
            listener.Start(); //开始监听端口,接收客户端请求
            text_info.Text = "Listening...";
 
            listener.BeginGetContext(ListenerHandle, listener);
 
 
        }
 
        //监听
        private void ListenerHandle(IAsyncResult result)
        {
            try
            {
                //listener = result.AsyncState as HttpListener;
                if (listener.IsListening)
                {
                    listener.BeginGetContext(ListenerHandle, result);
                    HttpListenerContext context = listener.EndGetContext(result);
                    //解析Request请求
                    HttpListenerRequest request = context.Request;
                    string content = "";
                    switch (request.HttpMethod)
                    {
                        case "POST":
                            {
                                Stream stream = context.Request.InputStream;
                                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                                content = reader.ReadToEnd();
                            }
                            break;
                        case "GET":
                            {
                                var data = request.QueryString;
                            }
                            break;
                    }
                    text_receive.Text += content;
 
                    //构造Response响应
                    HttpListenerResponse response = context.Response;
                    response.StatusCode = 200;
                    response.ContentType = "application/json;charset=UTF-8";
                    response.ContentEncoding = Encoding.UTF8;
                    response.AppendHeader("Content-Type", "application/json;charset=UTF-8");
 
                    using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8))
                    {
                        writer.Write("");
                        writer.Close();
                        response.Close();
                    }
                }
 
            }
            catch (Exception ex)
            {
                text_receive.Text += ex.Message;
            }
        }
    }
}

  

posted @ 2023-08-08 08:10  txwtech  阅读(72)  评论(0编辑  收藏  举报