国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net 截取Http請求的實現代碼

2020-01-18 00:49:06
字體:
來源:轉載
供稿:網友
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方法進行異步處理,在這個方法的內部首先實現的就是重新監聽。然后進行自己的處理。
呵呵,這段代碼有什么其他用處待考慮。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 介休市| 石首市| 都匀市| 铜陵市| 教育| 青浦区| 长顺县| 乡宁县| 繁昌县| 乌兰察布市| 内丘县| 平乡县| 崇左市| 岳西县| 阳城县| 河西区| 余江县| 万全县| 积石山| 门源| 旬阳县| 鄂伦春自治旗| 沛县| 邹平县| 呼图壁县| 临安市| 盱眙县| 泸州市| 嘉荫县| 阿拉善左旗| 久治县| 徐闻县| 奉新县| 章丘市| 铜陵市| 扶余县| 邵阳市| 周宁县| 仁寿县| 丁青县| 青川县|