zoukankan      html  css  js  c++  java
  • 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;
                }
            }
        }
    }
    

      

  • 相关阅读:
    vue excel 二进制文件导出
    小程序 input 批量监听
    vue-cli3 环境配置
    vue 同一浏览器只允许登录一个账号的解决办法
    vue ueditor 百度富文本
    视频分享
    vue、react、mpvue、node、ng视频教程以及项目
    【转】 值得推荐的C/C++框架和库 (真的很强大)
    C# 验证过滤代理IP是否有效
    【转】 C#学习笔记14——Trace、Debug和TraceSource的使用以及日志设计
  • 原文地址:https://www.cnblogs.com/webttt/p/14626218.html
Copyright © 2011-2022 走看看