全国统一热线:
028-86758058
18980748058
购买流程
付款方式
常见问题
在线提问
续租服务
购物车(
0
件)
用户名:
密 码:
记住
首 页
HOME
域名注册
DOMAIN
虚拟主机
WEB HOST
成品网站超市
AUTO Site
VPS主机
VPS SERVER
云 主 机
CLOUD HOST
租用托管
SERVER
海外主机
HK HOST
代理专区
AGENT
客服中心
SERVICE
站长教程
SERVICE
欢迎光临西部数据云计算中心,我们将竭诚为您提供最优质的服务!
中文域名
域名转入
域名转出
DNS管理
动态域名
获取域名证书
域名停放
域名过户
集群主机
双线主机
基本主机
港台主机
论坛主机
Linux主机
Vip合租主机
超G型主机
ASP.net主机
Java主机
智能建站主机
网店主机
美国主机
数据库
成品网站超市
智能建站主机
集群VPS主机
国内VPS主机
香港VPS主机
美国VPS主机
云主机介绍
云主机购买
服务器租用
主机托管
常见问题
香港主机
港台主机
美国主机
国内免备案
步骤流程
代理级别
代理合同
代理模式
代理优势
在线申请
产品列表
常见问题
代理商分布图
常见问题
有问必答
跟踪提问
购买流程
产品价格
付款方式
常用软件
网站备案
续租服务
汇款确认
相关文档
联系我们
域名资讯
主机资讯
行业动态
网页制作
php教程
mysql教程
域名资讯
云计算资讯
网络编程
您当前的位置:
首页
>
网络编程
>
Net教程
asp.net 截取Http请求的实现代码
时间:2015-01-29 来源:互联网 作者:佚名
1:前言
本篇文章比较短,主要是因为我的一个随想产生的一段代码。 这段代码的功能你可以叫做是简单的Http
服务器
也可以叫做Http请求截取。它实现的功能就是截取Http请求然后自己做处理。
2:代码
复制代码
代码如下:
public class HttpServer : IDisposable
{
private HttpListener listener;
public void Start()
{
listener = new HttpListener();
listener.Prefixes.Add("http://localhost/");
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication | AuthenticationSchemes.Anonymous;
listener.Start();
listener.BeginGetContext(GetContext, null);
}
private void GetContext(IAsyncResult ar)
{
HttpListenerRequest Request;
HttpListenerResponse Response;
try
{
HttpListenerContext ctx = listener.EndGetContext(ar);
Request = ctx.Request;
Response = ctx.Response;
//setup waiting for the next request
listener.BeginGetContext(GetContext, null);
}
catch (InvalidOperationException)
{
return;
}
catch (HttpListenerException)
{
return;
}
try
{
var sw = new StreamWriter(Response.OutputStream);
sw.Write(@"<html><body><p>你的请求已经被截取</p></body></html>");
sw.Flush();
}
finally
{
Response.OutputStream.Flush();
Response.Close();
}
}
public void Dispose()
{
if (listener != null)
listener.Stop();
}
}
3:简单解释一下
代码的核心就是HttpListener,通过它去侦听一个端口,当有请求的时候BeginGetContext交给GetContext方法进行异步处理,在这个方法的内部首先实现的就是重新监听。然后进行自己的处理。
呵呵,这段代码有什么其他用处待考虑。
来顶一下
返回首页
推荐资讯
ASP.NET 应用程序级 验证用户是否登
大家应该都希望有一种应用程序级的简单处理程序,今天刚好被
asp.net B2B网站对接支付宝接口
大型网上购物系统除了能让会员选择货到付款结账方式外,还应
相关文章
asp.net B2B网站对接支付宝接口
asp.net 页面编码常见问题小结
Asp.net开发常用的51个非常实用的代码
asp.net通过Ajax UpdatePanel回传后滚动条位置变更解决
ASP.NET 应用程序级 验证用户是否登录 一般处理程序
asp.net获取当前网址url的各种属性(文件名、参数、域名
asp.net 获取目录下的文件数和文件夹数
Asp.Net 网站性能优化之缓字决 (上) 缓冲写数据
asp.net Gridview数据列中实现鼠标悬浮变色
asp.net String.format中大括号的加入方法
栏目更新
栏目热门
返回首页
关于我们
联系我们
付款方式
价格总览
资讯中心
友情链接
媒体关注
有问必答
投诉建议
网站备案
《中华人民共和国增值电信业务经营许可证》编号:川B2-20080058号
官方网址:
xibushuju.com
西部数据
Copyright © 2002~2015
天府快车
版权所有
电话总机:
028-86758058
(50线) 传真:
028-86758058