asp.net 截取Http請(qǐng)求的實(shí)現(xiàn)代碼
更新時(shí)間:2010年06月01日 18:23:16 作者:
本篇文章比較短,主要是因?yàn)槲业囊粋€(gè)隨想產(chǎn)生的一段代碼。 這段代碼的功能你可以叫做是簡(jiǎn)單的Http服務(wù)器也可以叫做Http請(qǐng)求截取。它實(shí)現(xiàn)的功能就是截取Http請(qǐng)求然后自己做處理。
1:前言
本篇文章比較短,主要是因?yàn)槲业囊粋€(gè)隨想產(chǎn)生的一段代碼。 這段代碼的功能你可以叫做是簡(jiǎn)單的Http服務(wù)器也可以叫做Http請(qǐng)求截取。它實(shí)現(xiàn)的功能就是截取Http請(qǐng)求然后自己做處理。
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>你的請(qǐng)求已經(jīng)被截取</p></body></html>");
sw.Flush();
}
finally
{
Response.OutputStream.Flush();
Response.Close();
}
}
public void Dispose()
{
if (listener != null)
listener.Stop();
}
}
3:簡(jiǎn)單解釋一下
代碼的核心就是HttpListener,通過(guò)它去偵聽(tīng)一個(gè)端口,當(dāng)有請(qǐng)求的時(shí)候BeginGetContext交給GetContext方法進(jìn)行異步處理,在這個(gè)方法的內(nèi)部首先實(shí)現(xiàn)的就是重新監(jiān)聽(tīng)。然后進(jìn)行自己的處理。
呵呵,這段代碼有什么其他用處待考慮。
本篇文章比較短,主要是因?yàn)槲业囊粋€(gè)隨想產(chǎn)生的一段代碼。 這段代碼的功能你可以叫做是簡(jiǎn)單的Http服務(wù)器也可以叫做Http請(qǐng)求截取。它實(shí)現(xiàn)的功能就是截取Http請(qǐng)求然后自己做處理。
2:代碼
復(fù)制代碼 代碼如下:
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>你的請(qǐng)求已經(jīng)被截取</p></body></html>");
sw.Flush();
}
finally
{
Response.OutputStream.Flush();
Response.Close();
}
}
public void Dispose()
{
if (listener != null)
listener.Stop();
}
}
3:簡(jiǎn)單解釋一下
代碼的核心就是HttpListener,通過(guò)它去偵聽(tīng)一個(gè)端口,當(dāng)有請(qǐng)求的時(shí)候BeginGetContext交給GetContext方法進(jìn)行異步處理,在這個(gè)方法的內(nèi)部首先實(shí)現(xiàn)的就是重新監(jiān)聽(tīng)。然后進(jìn)行自己的處理。
呵呵,這段代碼有什么其他用處待考慮。
相關(guān)文章
手動(dòng)把a(bǔ)sp.net的類生成dll文件的方法
當(dāng)我們?cè)陂_(kāi)發(fā)的時(shí)候,有時(shí)會(huì)將一些方法封裝起來(lái)供別人調(diào)用,下面就是一種生成DLL的方法.2009-11-11
WPF開(kāi)發(fā)之利用DrawingVisual繪制高性能曲線圖
通過(guò)WPF實(shí)現(xiàn)大數(shù)據(jù)曲線圖時(shí),如果用最基礎(chǔ)的Canvas來(lái)實(shí)現(xiàn),性能堪憂。所以本文將利用DrawingVisual繪制高性能曲線圖,感興趣的可以了解一下2022-02-02
.Net判斷一個(gè)對(duì)象是否為數(shù)值類型實(shí)例
這篇文章主要介紹了.Net判斷一個(gè)對(duì)象是否為數(shù)值類型的方法,實(shí)例講述了一個(gè)國(guó)外的示例并對(duì)其進(jìn)行了改進(jìn),非常實(shí)用,需要的朋友可以參考下2014-10-10
.Net中MoongoDB的簡(jiǎn)單調(diào)用圖文教程
這篇文章主要給大家介紹了關(guān)于.Net中MoongoDB的簡(jiǎn)單調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
asp.net AJAX實(shí)現(xiàn)無(wú)刷新獲得數(shù)據(jù)
提供一個(gè)使用AJAX實(shí)現(xiàn)無(wú)刷新判斷注冊(cè)用戶名是否被注冊(cè)的代碼:2008-11-11
asp.net 頁(yè)面回跳實(shí)現(xiàn)代碼
今天做登錄時(shí),遇到點(diǎn)小問(wèn)題,在網(wǎng)上找了一下,沒(méi)看到源碼案例,不過(guò)還是花了一點(diǎn)時(shí)間調(diào)試通過(guò)了在此記錄一下,備忘。2010-03-03
ASP.NET Core中調(diào)整HTTP請(qǐng)求大小的幾種方法詳解
這篇文章主要給大家介紹了關(guān)于在ASP.NET Core中如何調(diào)整HTTP請(qǐng)求大小的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12

