先把代码放在这里,下面再详细解说:

  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using Oracle.DataAccess.Client;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Web;
  16.  
  17. namespace ConsoleApplication1
  18. {
  19. class Program
  20. {
  21. static Object o = new object();
  22. static void Main(string[] args)
  23. {
  24. HttpListener listerner = new HttpListener();
  25. while (true)
  26. {
  27. try
  28. {
  29. listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
  30. listerner.Prefixes.Add("http://127.0.0.1:1500/Service/");
  31. listerner.Start();
  32. }
  33. catch (Exception ex)
  34. {
  35. Console.WriteLine("服务启动失败...");
  36. break;
  37. }
  38. Console.WriteLine("服务器启动成功.......");
  39.  
  40. //线程池
  41. int minThreadNum;
  42. int portThreadNum;
  43. int maxThreadNum;
  44. ThreadPool.GetMaxThreads(out maxThreadNum, out portThreadNum);
  45. ThreadPool.GetMinThreads(out minThreadNum, out portThreadNum);
  46. Console.WriteLine("最大线程数:{0}", maxThreadNum);
  47. Console.WriteLine("最小空闲线程数:{0}", minThreadNum);
  48. //ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc1), x);
  49.  
  50. Console.WriteLine("\n\n等待客户连接中。。。。");
  51. while (true)
  52. {
  53. //等待请求连接
  54. //没有请求则GetContext处于阻塞状态
  55. HttpListenerContext ctx = listerner.GetContext();
  56.  
  57. ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc), ctx);
  58. }
  59. //listerner.Stop();
  60. }
  61.  
  62. Console.ReadKey();
  63. }
  64.  
  65. static void TaskProc(object o)
  66. {
  67. HttpListenerContext ctx = (HttpListenerContext)o;
  68.  
  69. ctx.Response.StatusCode = ;//设置返回给客服端http状态代码
  70.  
  71. //接收Get参数
  72. string type = ctx.Request.QueryString["type"];
  73. string userId = ctx.Request.QueryString["userId"];
  74. string password = ctx.Request.QueryString["password"];
  75. string filename = Path.GetFileName(ctx.Request.RawUrl);
  76. string userName = HttpUtility.ParseQueryString(filename).Get("userName");//避免中文乱码
  77. //进行处理
  78. Console.WriteLine("收到数据:" + userName);
  79.  
  80. //接收POST参数
  81. Stream stream = ctx.Request.InputStream;
  82. System.IO.StreamReader reader = new System.IO.StreamReader(stream, Encoding.UTF8);
  83. String body = reader.ReadToEnd();
  84. Console.WriteLine("收到POST数据:" + HttpUtility.UrlDecode(body));
  85. Console.WriteLine("解析:" + HttpUtility.ParseQueryString(body).Get("userName"));
  86.  
  87. //使用Writer输出http响应代码,UTF8格式
  88. using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream,Encoding.UTF8))
  89. {
  90. writer.Write("处理结果,Hello world<br/>");
  91. writer.Write("数据是userId={0},userName={1}", userId, userName);
  92. writer.Close();
  93. ctx.Response.Close();
  94. }
  95. }
  96.  
  97. }
  98.  
  99. }

1.可通过HttpUtility.UrlDecode对传入的参数进行解码,防止中文乱码

2.StreamWriter必须使用UTF8格式,防止中文乱码

3.微软提供的HttpListener默认不能接收POST参数,所以需要自己去解析,上面已实现

4.界面可通过form的post方式直接提交数据

原文地址:http://blog.sina.com.cn/s/blog_6f3ff2c90101wwh5.html

C# 监听HTTP请求的更多相关文章

  1. 李洪强iOS开发本人集成环信的经验总结_07_监听好友请求

    李洪强iOS开发本人集成环信的经验总结_07_监听好友请求 来到Appdalegate中: 遵守代理协议 设置代理  实现监听好友请求的回调的方法

  2. 怎样监听HTTP请求的发出与完成

    1. 监听HTTP请求发出的事件是: xhr.onloadstart 2. 监听HTTP请求结束的事件是: xhr.onloadend xhr.onloadstart = function() { / ...

  3. Spring Boot实现一个监听用户请求的拦截器

    项目中需要监听用户具体的请求操作,便通过一个拦截器来监听,并继续相应的日志记录 项目构建与Spring Boot,Spring Boot实现一个拦截器很容易. Spring Boot的核心启动类继承W ...

  4. nginx源代码分析--监听套接字的创建 套接字的监听 HTTP请求创建连接

    作为一个webserver,那么肯定是有监听套接字的,这个监听套接字是用于接收HTTP请求的,这个监听套接字的创建是依据配置文件的内容来创建的,在nginx.conf文件里有多少个地址就须要创建多少个 ...

  5. 怎样监听HTTP请求的成功、失败与进行时

    1. 监听请求成功: xhr.onload 2. 监听请求失败: xhr.onerror 3. 监听请求数据下载中: xhr.onprogress xhr.onload = function() { ...

  6. j2ee之监听页面请求

    本博客的起因是我想监听浏览器端每个页面都访问了哪些资源~~: 我是个菜鸡,所以我要记在我的小本本上,我怕忘了又~~~: 代码我是写在springboot2.1中的,有兴趣的同学可以玩一下~ 1:代码如 ...

  7. js 监听ajax请求

    function hookSend(hook) { if (!XMLHttpRequest.prototype._oldSend) XMLHttpRequest.prototype._oldSend ...

  8. Fiddler监听Https请求响应

    Fiddler问题 - creation of the root certificate was not successful 解决办法: http://localhost:8888/    安装证书 ...

  9. 监听HTTP请求

    using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Oracle.DataAccess.Client; using System; usi ...

随机推荐

  1. python 列表复制给另一个列表,改值两个列表均会改变(备忘)

    http://blog.csdn.net/lc_lc2000/article/details/53135839 本意是使A = B,B为一个列表,结果在后续对A的操作中,导致B中的值也改变了,才回忆起 ...

  2. Spring MVC和Spring Boot的理解以及比较

    Spring MVC是什么?(1)Spring MVC是Spring提供的一个强大而灵活的模块式web框架.通过Dispatcher Servlet, ModelAndView 和 View Reso ...

  3. leetcode283

    public class Solution { public void MoveZeroes(int[] nums) { ; ; i < nums.Length; i++) { //[0, 1, ...

  4. Redis内存模型总结

    一.Redis内存统计 在客户端通过redis-cli连接服务器后,通过info命令可以查看内存使用情况: info memory 返回结果中比较重要的几个说明如下: (1)used_memory:R ...

  5. jsonArray返回

    dao <select id="selectShopInfo" resultType="java.util.HashMap"> SELECT * F ...

  6. Oracle,cast函数

    cast(要转换的值 AS 转换的类型): 问题:'            ' as FSubBillNo 若用此法 oracle 默认字段类型为char型 且字段长度度为输入空格的长度,会导致字符串 ...

  7. [PHP]误读支付宝接口可能引发的乌龙

    ------------------------------------------------------------------------------------ 之所以发现这个坑,源起项目中的 ...

  8. Python文件操作---合并文本文件内容

    目前一个用的比较多的功能:将多个小文件的内容合并在一个统一的文件中,对原始文件重命名标记其已被处理过.之前使用其他脚本写的,尝试用python写了一下,顺便熟悉一下python的文件处理命令. 原始文 ...

  9. IntelliJ IDEA——SVN的配置及使用

    服务端:VisualSVN-Server-3.9.1-x64 下载地址:https://www.visualsvn.com/server/download/ TortoiseSVN 安装 下载地址:h ...

  10. Jekins在Tomcat上的安装和配置

    首先,apache.org的官网下载Apache Tomcat. 第二:点击/一步一步的安装tomcat,没有任何需要说明的难点. 我偏好选择安装tomcat可执行文件,这样可以在安装时就自动吧tom ...