亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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:代碼
復(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)文章

最新評(píng)論