c# HttpListener 使用
与 IIS 上发布网站相比,使用 HttpListener 编程的程序更加轻量化,易于发布和更新。配合 Thread 或 Task 类也可满足一定的并发。
https://docs.microsoft.com/zh-cn/dotnet/api/system.net.httplistener?view=netframework-4.7.2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Threading;
using System.IO;
//https://docs.microsoft.com/zh-cn/dotnet/api/system.net.httplistener?view=netframework-4.7.2 namespace WebServer
{
class Program
{
static void Main(string[] args)
{
try
{
using (HttpListener listener = new HttpListener())
{
listener.Prefixes.Add("http://localhost:8888/");
listener.Start();
Console.WriteLine("开始监听");
while (true)
{
try
{
HttpListenerContext context = listener.GetContext();//阻塞
HttpListenerRequest request = context.Request;
string postData = new StreamReader(request.InputStream).ReadToEnd();
Console.WriteLine("收到请求:" + postData);
HttpListenerResponse response = context.Response;//响应
string responseBody = "响应";
response.ContentLength64 = System.Text.Encoding.UTF8.GetByteCount(responseBody);
response.ContentType = "text/html; Charset=UTF-8";
//输出响应内容
Stream output = response.OutputStream;
using (StreamWriter sw = new StreamWriter(output))
{
sw.Write(responseBody);
}
Console.WriteLine("响应结束");
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
}
}
}
catch (Exception err)
{
Console.WriteLine("程序异常,请重新打开程序:" + err.Message);
}
}
}
}
c# HttpListener 使用的更多相关文章
- 通过HttpListener实现简单的Http服务
使用HttpListener实现简单的Http服务 HttpListener提供一个简单的.可通过编程方式控制的 HTTP 协议侦听器.使用它可以很容易的提供一些Http服务,而无需启动IIS这类大型 ...
- Httplistener Access Denied
HttpListener.Start() 会出现HttpListenerException, 显示拒绝访问 一般是因为有些计算机账户是没有权限创建 HttpListener服务, 但是可以注册一些规则 ...
- VS调试在Win7(vista系列)操作系统下 HttpListener拒绝访问解决办法
一. VS调试在Win7(vista系列)操作系统下 HttpListener无法绑定多个 指定IP.端口问题 来自:http://www.cnblogs.com/ryhan/p/4195693.ht ...
- atitit.跨架构 bs cs解决方案. 自定义web服务器的实现方案 java .net jetty HttpListener
atitit.跨架构 bs cs解决方案. 自定义web服务器的实现方案 java .net jetty HttpListener 1. 自定义web服务器的实现方案,基于原始socket vs ...
- 利用HttpListener创建简单的HTTP服务
using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using ...
- 基于HttpListener的web服务器
写在前面 前面两篇文章分别介绍了基于原始socket的web服务器和基于tcpListener的web服务器,本篇文章将继续介绍另外一种基于HttpListener的. HttpListener Ht ...
- HttpListener 实现web服务端
1. using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...
- 跟随上次的socket sever,追加Tcplistener、Httplistener的server
一.Tcplistener搭建web server 1.同socket类似,Tcplistener其实是对socket的封装,方便编程,先初始化tcplistener并且开始监听 //初始化端点信息 ...
- win7中用 httplistener 出现 503 错误的问题
项目中须要用httplistener提供一个简单的httpserver服务.可是执行都是提示: UnHandledException Message:拒绝訪问 在System.Net.HttpList ...
- HttpListener 实现web服务器
一.使用方法 1. Start()方法 允许此实例接受传入的请求.即开始监听 2. Stop()方法 处理完所有当前排队的请求后关闭HttpListener对象 3. GetContext()方法 ...
随机推荐
- rpm2cpio - 从 RPM 软件包中提取 cpio 归档
SYNOPSIS rpm2cpio [filename] DESCRIPTION rpm2cpio 将指定的一个 .rpm 文件转换为一个 cpio 文档,输出到标准输出.如果给出了 `-' 参数,那 ...
- ssh - OpenSSH SSH 客户端 (远程登录程序)
总览 (SYNOPSIS) ssh [-l login_name ] hostname | user@hostname [command ] ssh -words [-afgknqstvxACNTX1 ...
- python类中方法__str__()和__repr__()简单粗暴总结
在交互式模式下,类中同时实现__str__()和__repr__()方法: 直接输入实例名称显示repr返回的类容: 用print打印实例名称显示str返回的内容: >>> clas ...
- 前端学习(三十五)模块化es6(笔记)
RequireJs:一.安装.下载 官网: requirejs.org Npm: npm i requirejs二.使用 以前的开发方式的问题: 1).js 是阻塞加 ...
- condition简单示例
在concurrent包中提供了condition接口,通过该接口可唤醒指定的某个线程,而不是采用随机唤醒的形式. import java.util.concurrent.locks.Conditio ...
- 您的加密USB驱动器是否安全?黑客又是如何攻破的?
您如何确定您使用的“安全”USB驱动器是否真的安全,并且您存储的数据无法提取?这正是Google公司和中国网络安全研究人员在最近的2018年黑帽美国大会上以艰难的方式攻击加密的USB密钥”的问题. 研 ...
- 15_1.InetAddress
import java.net.InetAddress; import java.net.UnknownHostException; public class InetAdressTest { pub ...
- python的strip和split函数
这两个函数都是string的类函数 1.strip是去掉字符串头尾的特定字符,分三个 aa=' bb=aa.rstrip(') cc=aa.lstrip(') dd=aa.strip(') print ...
- Angular JS - 8 - SeaJS与前端模块化
一.前端模块化 关于前端模块化,参考以下链接 : https://github.com/seajs/seajs/issues/547 http://www.cnblogs.com/huiguo/cat ...
- 关于12306Bypass-分流抢票
12306Bypass-分流抢票-2013-15年 官网:http://www.12306bypass.com 分流抢票是一款完全免费的抢票软件,请抵制淘宝贩卖等诈骗行为 作者不会授 ...