监听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方式直接提交数据
5.[System.Net.HttpListenerException] = {"拒绝访问。"}问题
原文地址:http://blog.sina.com.cn/s/blog_6f3ff2c90101wwh5.html
参考地址:https://www.cnblogs.com/duanjt/p/5566336.html
监听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/ 安装证书 ...
- C# 监听HTTP请求
先把代码放在这里,下面再详细解说: using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Oracle.DataAccess.Client; ...
随机推荐
- bind和on的区别
bind方法与on方法都是事件绑定,但是两者却又有着一个大区别:事件委托 jquery文档中bind和on函数绑定事件的用法: .bind(events [,eventData], handler) ...
- SCRUM管理之KPI与OKRs结合
以下内容是本人在担任ScrumMaster中的实际应用,供大家交流学习参考. 目录 1绩效考核原则 4 2绩效考核范围 4 3绩效计算 4 3.1KPI 4 3.1.1KPI指标评分表 4 3.1.2 ...
- 53-java中的queue
java.util 接口 Queue<E> 类型参数: E - collection 中所保存元素的类型. 所有超级接口: Collection<E>, Iterable< ...
- CSS网页布局
注:优化样式表:增加css样式表的可读性 减伤样式重复 一.主要内容 1.布局分类;131 121 2.display属性排版 3.float属性排版(横向多列布局) 4.防止父类盒子塌陷 二.标 ...
- vue项目获取地址栏参数(非路由传参)
在项目中,遇到一个需求,就是另一个系统直接跳转到我们项目中的某个页面,不需要做用户的校验直接单纯的跳转新页面,再初始化查询数据,参数以地址栏的形式传入 由于原来项目做过权限控制,所以在路由那边需要进行 ...
- MWeb for Mac使用教程-如何在文档库中快速搜索
使用MWeb for Mac专业的 Markdown 编辑写作软件,可以让你随时记录自己的想法,灵感,创意,为您的工作节省宝贵的时间.本篇文章带来的是MWeb for Mac如何在文档库中快速搜索使用 ...
- git 提交代码操作
1.修改1分支后 git add git commint2.切换到本地分支git checkout local-5.0git remote update 更新远程仓库3.git pull origin ...
- 第三次Java作
- 从阿里巴巴面试题到java类加载机制
首先很经典的阿里巴巴面试题 加上我自己的一些疑惑代码 public class Text { public static int k = 0; public final int k1 = 3; //自 ...
- github菜鸟入门
github菜鸟入门教程 闲来无事,研究了下github的玩法,完毕总结:简单好玩,上档次! 一.首先注册github的账号 二.下载安装git 三.新建仓库 1.点击右上方的+号选择首项新建仓库 2 ...