asp.net 截取Http請求的實(shí)現(xiàn)代碼
更新時(shí)間:2010年06月01日 18:23:16 作者:
本篇文章比較短,主要是因?yàn)槲业囊粋€(gè)隨想產(chǎn)生的一段代碼。 這段代碼的功能你可以叫做是簡單的Http服務(wù)器也可以叫做Http請求截取。它實(shí)現(xiàn)的功能就是截取Http請求然后自己做處理。
1:前言
本篇文章比較短,主要是因?yàn)槲业囊粋€(gè)隨想產(chǎn)生的一段代碼。 這段代碼的功能你可以叫做是簡單的Http服務(wù)器也可以叫做Http請求截取。它實(shí)現(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,通過它去偵聽一個(gè)端口,當(dāng)有請求的時(shí)候BeginGetContext交給GetContext方法進(jìn)行異步處理,在這個(gè)方法的內(nèi)部首先實(shí)現(xiàn)的就是重新監(jiān)聽。然后進(jìn)行自己的處理。
呵呵,這段代碼有什么其他用處待考慮。
本篇文章比較短,主要是因?yàn)槲业囊粋€(gè)隨想產(chǎn)生的一段代碼。 這段代碼的功能你可以叫做是簡單的Http服務(wù)器也可以叫做Http請求截取。它實(shí)現(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,通過它去偵聽一個(gè)端口,當(dāng)有請求的時(shí)候BeginGetContext交給GetContext方法進(jìn)行異步處理,在這個(gè)方法的內(nèi)部首先實(shí)現(xiàn)的就是重新監(jiān)聽。然后進(jìn)行自己的處理。
呵呵,這段代碼有什么其他用處待考慮。
相關(guān)文章
EF使用Code First模式給實(shí)體類添加復(fù)合主鍵
這篇文章介紹了EF使用Code First模式給實(shí)體類添加復(fù)合主鍵的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03C#中的Equals、RefrenceEquals和==的區(qū)別與聯(lián)系
C#中判斷兩個(gè)對象是否相等有Equals、RefrenceEquals和==三種,其中==為運(yùn)算符,其它兩個(gè)為方法,而Equals又有兩種版本,一個(gè)是靜態(tài)的,一個(gè)是虛擬的,詳細(xì)了解可以參考本文2012-12-12.NET異步編程總結(jié)----四種實(shí)現(xiàn)模式代碼總結(jié)
本篇文章主要介紹了.NET異步編程總結(jié)----四種實(shí)現(xiàn)模式,詳細(xì)的介紹了每種方法的實(shí)現(xiàn)和實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12ASP.NET對SQLServer的通用數(shù)據(jù)庫訪問類
這篇文章主要實(shí)現(xiàn)了ASP.NET對SQLServer的通用數(shù)據(jù)庫訪問類2016-02-02.NETCore基于RabbitMQ實(shí)現(xiàn)延時(shí)隊(duì)列的兩方法
這篇文章主要介紹了.NETCore基于RabbitMQ實(shí)現(xiàn)延時(shí)隊(duì)列的兩方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09