C# 创建HttpServer
来源:互联网 发布:河南豫广网络有线电视 编辑:程序博客网 时间:2024/05/16 18:35
时间:2016.10.28
在C#中使用HttpListener实现HttpServer的简单示例
[ Program.cs ]
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Threadingnamespace httpServer{ class Program { static void Main(string[] args) { HttpListener httpListener; httpListener = new HttpListener(); httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; httpListener.Prefixes.Add("http://127.0.0.1:8086/"); httpListener.Start(); new Thread( new ThreadStart( delegate { try{ loop(httpListener); } catch(Exception) { httpListener.Stop(); } })).Start(); } private static void loop(HttpListener httpListener) { while (true) { HttpListenerContext context = httpListener.GetContext(); HttpListenerRequest hRequest = context.Request; HttpListenerResponse hResponse = context.Response; if (hRequest.HttpMethod == "POST") { Console.WriteLine("POST:" + hRequest.Url); byte[] res = Encoding.UTF8.GetBytes("OK"); hResponse.OutputStream.Write(res, 0, res.Length); } else if (hRequest.HttpMethod == "GET") { Console.WriteLine( "GET:"+hRequest.Url); byte[] res = Encoding.UTF8.GetBytes("OK"); hResponse.OutputStream.Write(res, 0, res.Length); } } httpListener.Close(); } }}
运行结果:
1). 在浏览器中输入 127.0.0.1:8086
2). 在浏览器中输入(本机IP) 192.168.40.178:8086 没有显示内容
总结:
… 1. HttpListener的GetContext是阻塞型的
问题:
Q1: httpListener.Prefixes.Add的时候如果端口号设置成80,则下面运行到httpListener.Start的时候会报异常?
Q2: 为什么本机IP的时候不现实内容?
0 0
- C# 创建HttpServer
- python 创建httpserver
- 用Python3创建httpServer
- HttpServer
- httpserver
- 使用python中的HTTPServer创建本地站点
- JDK6中httpserver创建http服务端实例
- Asp.net web Api源码分析-HttpServer的创建
- netbox问题, 无法创建NetBox.HttpServer,错误800A01AD
- Go语言httpServer创建路由的几种方式
- Java:使用Java内置的HTTPServer类创建web服务
- Java HttpServer
- Httpserver跳转
- java httpserver
- python HTTPServer
- iso httpServer
- HTTPServer入门
- HttpClient -- HttpServer
- OpenCV Tutorial: 像素強度變換(convertTo)
- Vuforia开发入门三-License Manager(证书管理器)
- HTTP 协议入门
- ARM linux内核启动时几个关键地址
- JS:字符串和数字之间的相互转换
- C# 创建HttpServer
- maven 本地仓库的配置以及如何修改默认.m2仓库位置
- 【Linux】使用update-alternatives命令进行版本的切换
- python-numpy-01
- leetcode_110. Balanced Binary Tree 判断一棵二叉树是否为平衡二叉树
- 【NOIP2015】Day2T3 运输计划
- JDK8 新特性流式数据处理
- 正确使用Android性能分析工具——TraceView
- 2016/10/28学习工作日志