当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net 截取Http请求的实现代码

asp.net 截取Http请求的实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

兴奋坠饰,51看电影,馨子男友

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方法进行异步处理,在这个方法的内部首先实现的就是重新监听。然后进行自己的处理。
呵呵,这段代码有什么其他用处待考虑。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网