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 ...
随机推荐
- uva-10041-水题
题意:有个人想找到个房子住,并且他经常去其他街道,输入n条街,给这个人选择一条街,使得他去其他街道的距离总和最小. 排序,双重for #include <string> #include& ...
- 编程最好用的字体consolas
python 自带idle最好用的字体consolas https://www.icourse163.org/learn/BIT-268001?tid=1002788003#/learn/forumd ...
- django之signal机制再探
djangobb中的signal post_save信号调用send函数时,为什么它会对与topic.post相关的其他models进行修改?同一个信号,例如post_save(保存过后的处理),是所 ...
- 为什么SQL用UPDATE语句更新时更新行数会多3行有触发器有触发器有触发器有触发器有触发器有触发器
update明显更新就一行,但是结果显示更新多行. 原因是有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器有触发器
- input 选择框改变背景小技巧
最近在项目中遇到一个问题,想要改变input选择框的背景,然而,令我没有想到的是,竟然无法直接改变背景的颜色 通常情况下:我们都可以通过改变元素的 background-color 的值来改变元素的背 ...
- python+appium-desktop:安卓(android)7.0以上使用appium无法定位元素(无法refresh)且 无法运行脚本
--解决方法: 启动appium时配置中添加: "automationName":"uiautomator2" --扩展: 想支持安卓7.0及以上版本需要满足一 ...
- MYSQL性能优化(1)
优化步骤 1.show status 查询服务器状态运行信息 根据增删改查统计信息可以知道数据库是查询为主还是更新为主,各类型业务大致比例(更新操作 执行与回滚都会计数) 对于事务,可以通过Com_c ...
- 使用jQuery+huandlebars判断类型的helper
兼容ie8(很实用,复制过来,仅供技术参考,更详细内容请看源地址:http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) & ...
- C++ 50学习 之提高对 C++的认识
转自Effective C++ 理解设计目标. 1.和 C 的兼容性. 2.效率. C++在效率上可以和 C 匹 敌 ---- 二者相差大约在 5%之内. 3.和传统开发工具及环境的兼容性. 4.解决 ...
- pycharm的断点调试【转自https://blog.csdn.net/weixin_39198406/article/details/78873120】
1. show execution point (F10)显示目前项目所有断点2. step over (F8)下一步但仅限于设置断点的文件3. step into (F7)执行下一行4. step ...