与 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 使用的更多相关文章

  1. 通过HttpListener实现简单的Http服务

    使用HttpListener实现简单的Http服务 HttpListener提供一个简单的.可通过编程方式控制的 HTTP 协议侦听器.使用它可以很容易的提供一些Http服务,而无需启动IIS这类大型 ...

  2. Httplistener Access Denied

    HttpListener.Start() 会出现HttpListenerException, 显示拒绝访问 一般是因为有些计算机账户是没有权限创建 HttpListener服务, 但是可以注册一些规则 ...

  3. VS调试在Win7(vista系列)操作系统下 HttpListener拒绝访问解决办法

    一. VS调试在Win7(vista系列)操作系统下 HttpListener无法绑定多个 指定IP.端口问题 来自:http://www.cnblogs.com/ryhan/p/4195693.ht ...

  4. atitit.跨架构 bs cs解决方案. 自定义web服务器的实现方案 java .net jetty  HttpListener

    atitit.跨架构 bs cs解决方案. 自定义web服务器的实现方案 java .net jetty  HttpListener 1. 自定义web服务器的实现方案,基于原始socket vs   ...

  5. 利用HttpListener创建简单的HTTP服务

    using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using ...

  6. 基于HttpListener的web服务器

    写在前面 前面两篇文章分别介绍了基于原始socket的web服务器和基于tcpListener的web服务器,本篇文章将继续介绍另外一种基于HttpListener的. HttpListener Ht ...

  7. HttpListener 实现web服务端

    1. using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...

  8. 跟随上次的socket sever,追加Tcplistener、Httplistener的server

    一.Tcplistener搭建web server 1.同socket类似,Tcplistener其实是对socket的封装,方便编程,先初始化tcplistener并且开始监听 //初始化端点信息 ...

  9. win7中用 httplistener 出现 503 错误的问题

    项目中须要用httplistener提供一个简单的httpserver服务.可是执行都是提示: UnHandledException Message:拒绝訪问 在System.Net.HttpList ...

  10. HttpListener 实现web服务器

    一.使用方法 1. Start()方法 允许此实例接受传入的请求.即开始监听 2. Stop()方法 处理完所有当前排队的请求后关闭HttpListener对象 3. GetContext()方法  ...

随机推荐

  1. Kintex7 XC7K325T 板卡五兄弟

    Kintex 7五兄弟 1.     基KC705E 增强版基于FMC接口的Xilinx Kintex-7 FPGA K7 XC7K325T PCIeX8 接口卡(136) 本板卡是Xilinx公司芯 ...

  2. linux篇之Nginx web服务器简单部署

    一.安装部署nginx 1. 部署前先对nginx介绍下别嫌BB: 如果你听说或使用过Apache软件,那么很快就会熟悉Nginx软件,与Apache软件类似, Nginx(“engine x”)是一 ...

  3. C Primer Plus 学习 第三章

    这里只记录我自己以前不懂得地方,明白的地方就略过了 位  字节  字 位    0,1 字节  8位  也就有8位0,1的组合   2的8次方的组合 字      设计计算机时给定的自然存储单元.8位 ...

  4. DFSORT

    1.1   Outline   I.             Introduction Overview 2.1    What is DFSORT? 2.2    Usage of DFSORT 2 ...

  5. HTML基础用 表格做报表

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. RequestMappingHandlerMapping详解

    我们先理简单梳理一个关系 关系梳理 spring ioc 是spring的核心,用来管理spring bean的生命周期 MVC 是一种使用 MVC(Model View Controller 模型- ...

  7. ssm科普篇

    springMVC执行步骤: 1.用户发送请求到前端控制器,前端控制器根据请求信息来决定选择页面控制器,并将请求委托给它 2.页面控制器收到请求后,进行功能处理,首先需要收集和绑定请求参数到一个对象, ...

  8. join()、split()

    join()用于把数组转化为字符串 var arr=['hello','world','kugou']; document.write(arr.join(''));//helloworldkugou ...

  9. Thymeleaf入门到吃灰

    Thymeleaf 官网部分翻译:反正就是各种好 Thymeleaf是用来开发Web和独立环境项目的服务器端的Java模版引擎 Spring官方支持的服务的渲染模板中,并不包含jsp.而是Thymel ...

  10. 【Eureka】实现原理

    Eureka Client 拉取Eureka Server中的全量注册表 注册自身实例InstanceInfo至Eureka Server 初始化定时任务 心跳(续约)任务 拉取增量注册表更新本地注册 ...