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