asp.net 截取Http請求的實現(xiàn)代碼
更新時間:2010年06月01日 18:23:16 作者:
本篇文章比較短,主要是因為我的一個隨想產(chǎn)生的一段代碼。 這段代碼的功能你可以叫做是簡單的Http服務(wù)器也可以叫做Http請求截取。它實現(xiàn)的功能就是截取Http請求然后自己做處理。
1:前言
本篇文章比較短,主要是因為我的一個隨想產(chǎn)生的一段代碼。 這段代碼的功能你可以叫做是簡單的Http服務(wù)器也可以叫做Http請求截取。它實現(xiàn)的功能就是截取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>你的請求已經(jīng)被截取</p></body></html>");
sw.Flush();
}
finally
{
Response.OutputStream.Flush();
Response.Close();
}
}
public void Dispose()
{
if (listener != null)
listener.Stop();
}
}
3:簡單解釋一下
代碼的核心就是HttpListener,通過它去偵聽一個端口,當(dāng)有請求的時候BeginGetContext交給GetContext方法進行異步處理,在這個方法的內(nèi)部首先實現(xiàn)的就是重新監(jiān)聽。然后進行自己的處理。
呵呵,這段代碼有什么其他用處待考慮。
本篇文章比較短,主要是因為我的一個隨想產(chǎn)生的一段代碼。 這段代碼的功能你可以叫做是簡單的Http服務(wù)器也可以叫做Http請求截取。它實現(xiàn)的功能就是截取Http請求然后自己做處理。
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>你的請求已經(jīng)被截取</p></body></html>");
sw.Flush();
}
finally
{
Response.OutputStream.Flush();
Response.Close();
}
}
public void Dispose()
{
if (listener != null)
listener.Stop();
}
}
3:簡單解釋一下
代碼的核心就是HttpListener,通過它去偵聽一個端口,當(dāng)有請求的時候BeginGetContext交給GetContext方法進行異步處理,在這個方法的內(nèi)部首先實現(xiàn)的就是重新監(jiān)聽。然后進行自己的處理。
呵呵,這段代碼有什么其他用處待考慮。
相關(guān)文章
WPF開發(fā)之利用DrawingVisual繪制高性能曲線圖
通過WPF實現(xiàn)大數(shù)據(jù)曲線圖時,如果用最基礎(chǔ)的Canvas來實現(xiàn),性能堪憂。所以本文將利用DrawingVisual繪制高性能曲線圖,感興趣的可以了解一下2022-02-02
asp.net AJAX實現(xiàn)無刷新獲得數(shù)據(jù)
提供一個使用AJAX實現(xiàn)無刷新判斷注冊用戶名是否被注冊的代碼:2008-11-11
ASP.NET Core中調(diào)整HTTP請求大小的幾種方法詳解
這篇文章主要給大家介紹了關(guān)于在ASP.NET Core中如何調(diào)整HTTP請求大小的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12

