C# 监听HTTP请求
先把代码放在这里,下面再详细解说:
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using Oracle.DataAccess.Client;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Reflection;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Web;
- namespace ConsoleApplication1
- {
- class Program
- {
- static Object o = new object();
- static void Main(string[] args)
- {
- HttpListener listerner = new HttpListener();
- while (true)
- {
- try
- {
- listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
- listerner.Prefixes.Add("http://127.0.0.1:1500/Service/");
- listerner.Start();
- }
- catch (Exception ex)
- {
- Console.WriteLine("服务启动失败...");
- break;
- }
- Console.WriteLine("服务器启动成功.......");
- //线程池
- int minThreadNum;
- int portThreadNum;
- int maxThreadNum;
- ThreadPool.GetMaxThreads(out maxThreadNum, out portThreadNum);
- ThreadPool.GetMinThreads(out minThreadNum, out portThreadNum);
- Console.WriteLine("最大线程数:{0}", maxThreadNum);
- Console.WriteLine("最小空闲线程数:{0}", minThreadNum);
- //ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc1), x);
- Console.WriteLine("\n\n等待客户连接中。。。。");
- while (true)
- {
- //等待请求连接
- //没有请求则GetContext处于阻塞状态
- HttpListenerContext ctx = listerner.GetContext();
- ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc), ctx);
- }
- //listerner.Stop();
- }
- Console.ReadKey();
- }
- static void TaskProc(object o)
- {
- HttpListenerContext ctx = (HttpListenerContext)o;
- ctx.Response.StatusCode = ;//设置返回给客服端http状态代码
- //接收Get参数
- string type = ctx.Request.QueryString["type"];
- string userId = ctx.Request.QueryString["userId"];
- string password = ctx.Request.QueryString["password"];
- string filename = Path.GetFileName(ctx.Request.RawUrl);
- string userName = HttpUtility.ParseQueryString(filename).Get("userName");//避免中文乱码
- //进行处理
- Console.WriteLine("收到数据:" + userName);
- //接收POST参数
- Stream stream = ctx.Request.InputStream;
- System.IO.StreamReader reader = new System.IO.StreamReader(stream, Encoding.UTF8);
- String body = reader.ReadToEnd();
- Console.WriteLine("收到POST数据:" + HttpUtility.UrlDecode(body));
- Console.WriteLine("解析:" + HttpUtility.ParseQueryString(body).Get("userName"));
- //使用Writer输出http响应代码,UTF8格式
- using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream,Encoding.UTF8))
- {
- writer.Write("处理结果,Hello world<br/>");
- writer.Write("数据是userId={0},userName={1}", userId, userName);
- writer.Close();
- ctx.Response.Close();
- }
- }
- }
- }
1.可通过HttpUtility.UrlDecode对传入的参数进行解码,防止中文乱码
2.StreamWriter必须使用UTF8格式,防止中文乱码
3.微软提供的HttpListener默认不能接收POST参数,所以需要自己去解析,上面已实现
4.界面可通过form的post方式直接提交数据
原文地址:http://blog.sina.com.cn/s/blog_6f3ff2c90101wwh5.html
C# 监听HTTP请求的更多相关文章
- 李洪强iOS开发本人集成环信的经验总结_07_监听好友请求
李洪强iOS开发本人集成环信的经验总结_07_监听好友请求 来到Appdalegate中: 遵守代理协议 设置代理 实现监听好友请求的回调的方法
- 怎样监听HTTP请求的发出与完成
1. 监听HTTP请求发出的事件是: xhr.onloadstart 2. 监听HTTP请求结束的事件是: xhr.onloadend xhr.onloadstart = function() { / ...
- Spring Boot实现一个监听用户请求的拦截器
项目中需要监听用户具体的请求操作,便通过一个拦截器来监听,并继续相应的日志记录 项目构建与Spring Boot,Spring Boot实现一个拦截器很容易. Spring Boot的核心启动类继承W ...
- nginx源代码分析--监听套接字的创建 套接字的监听 HTTP请求创建连接
作为一个webserver,那么肯定是有监听套接字的,这个监听套接字是用于接收HTTP请求的,这个监听套接字的创建是依据配置文件的内容来创建的,在nginx.conf文件里有多少个地址就须要创建多少个 ...
- 怎样监听HTTP请求的成功、失败与进行时
1. 监听请求成功: xhr.onload 2. 监听请求失败: xhr.onerror 3. 监听请求数据下载中: xhr.onprogress xhr.onload = function() { ...
- j2ee之监听页面请求
本博客的起因是我想监听浏览器端每个页面都访问了哪些资源~~: 我是个菜鸡,所以我要记在我的小本本上,我怕忘了又~~~: 代码我是写在springboot2.1中的,有兴趣的同学可以玩一下~ 1:代码如 ...
- js 监听ajax请求
function hookSend(hook) { if (!XMLHttpRequest.prototype._oldSend) XMLHttpRequest.prototype._oldSend ...
- Fiddler监听Https请求响应
Fiddler问题 - creation of the root certificate was not successful 解决办法: http://localhost:8888/ 安装证书 ...
- 监听HTTP请求
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Oracle.DataAccess.Client; using System; usi ...
随机推荐
- python 列表复制给另一个列表,改值两个列表均会改变(备忘)
http://blog.csdn.net/lc_lc2000/article/details/53135839 本意是使A = B,B为一个列表,结果在后续对A的操作中,导致B中的值也改变了,才回忆起 ...
- Spring MVC和Spring Boot的理解以及比较
Spring MVC是什么?(1)Spring MVC是Spring提供的一个强大而灵活的模块式web框架.通过Dispatcher Servlet, ModelAndView 和 View Reso ...
- leetcode283
public class Solution { public void MoveZeroes(int[] nums) { ; ; i < nums.Length; i++) { //[0, 1, ...
- Redis内存模型总结
一.Redis内存统计 在客户端通过redis-cli连接服务器后,通过info命令可以查看内存使用情况: info memory 返回结果中比较重要的几个说明如下: (1)used_memory:R ...
- jsonArray返回
dao <select id="selectShopInfo" resultType="java.util.HashMap"> SELECT * F ...
- Oracle,cast函数
cast(要转换的值 AS 转换的类型): 问题:' ' as FSubBillNo 若用此法 oracle 默认字段类型为char型 且字段长度度为输入空格的长度,会导致字符串 ...
- [PHP]误读支付宝接口可能引发的乌龙
------------------------------------------------------------------------------------ 之所以发现这个坑,源起项目中的 ...
- Python文件操作---合并文本文件内容
目前一个用的比较多的功能:将多个小文件的内容合并在一个统一的文件中,对原始文件重命名标记其已被处理过.之前使用其他脚本写的,尝试用python写了一下,顺便熟悉一下python的文件处理命令. 原始文 ...
- IntelliJ IDEA——SVN的配置及使用
服务端:VisualSVN-Server-3.9.1-x64 下载地址:https://www.visualsvn.com/server/download/ TortoiseSVN 安装 下载地址:h ...
- Jekins在Tomcat上的安装和配置
首先,apache.org的官网下载Apache Tomcat. 第二:点击/一步一步的安装tomcat,没有任何需要说明的难点. 我偏好选择安装tomcat可执行文件,这样可以在安装时就自动吧tom ...