½¨Õ¾Ñ§ - ÇáËɽ¨Õ¾´Ó´Ë¿ªÊ¼£¡

½¨Õ¾Ñ§-¸öÈ˽¨Õ¾Ö¸ÄÏ,ÍøÒ³ÖÆ×÷,ÍøÕ¾Éè¼Æ,ÍøÕ¾ÖÆ×÷½Ì³Ì

asp.net ½ØÈ¡HttpÇëÇóµÄʵÏÖ´úÂë

ʱ¼ä:2010-06-01 21:31À´Ô´: ×÷Õß: µã»÷: ´Î
±¾ÆªÎÄÕ±Ƚ϶̣¬Ö÷ÒªÊÇÒòΪÎÒµÄÒ»¸öËæÏë²úÉúµÄÒ»¶Î´úÂë¡£ Õâ¶Î´úÂëµÄ¹¦ÄÜÄã¿ÉÒÔ½Ð×öÊǼòµ¥µÄHttp·þÎñÆ÷Ò²¿ÉÒÔ½Ð×öHttpÇëÇó½ØÈ¡¡£ËüʵÏֵŦÄܾÍÊǽØÈ¡HttpÇëÇóÈ»ºó×Ô¼º×ö´¦Àí¡£

1£ºÇ°ÑÔ
±¾ÆªÎÄÕ±Ƚ϶̣¬Ö÷ÒªÊÇÒòΪÎÒµÄÒ»¸öËæÏë²úÉúµÄÒ»¶Î´úÂë¡£ Õâ¶Î´úÂëµÄ¹¦ÄÜÄã¿ÉÒÔ½Ð×öÊǼòµ¥µÄHttp·þÎñÆ÷Ò²¿ÉÒÔ½Ð×öHttpÇëÇó½ØÈ¡¡£ËüʵÏֵŦÄܾÍÊǽØÈ¡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>ÄãµÄÇëÇóÒѾ­±»½ØÈ¡</p></body></html>");
sw.Flush();
}
finally
{
Response.OutputStream.Flush();
Response.Close();
}
}
public void Dispose()
{
if (listener != null)
listener.Stop();
}
}


3£º¼òµ¥½âÊÍÒ»ÏÂ
´úÂëµÄºËÐľÍÊÇHttpListener£¬Í¨¹ýËüÈ¥ÕìÌýÒ»¸ö¶Ë¿Ú£¬µ±ÓÐÇëÇóµÄʱºòBeginGetContext½»¸øGetContext·½·¨½øÐÐÒì²½´¦Àí£¬ÔÚÕâ¸ö·½·¨µÄÄÚ²¿Ê×ÏÈʵÏֵľÍÊÇÖØмàÌý¡£È»ºó½øÐÐ×Ô¼ºµÄ´¦Àí¡£
ºÇºÇ£¬Õâ¶Î´úÂëÓÐʲôÆäËûÓô¦´ý¿¼ÂÇ¡£

(ÔðÈα༭£ºadmin)
Ö¯ÃζþάÂëÉú³ÉÆ÷
¶¥Ò»ÏÂ
(1)
100%
²ÈÒ»ÏÂ
(0)
0%
------·Ö¸ôÏß----------------------------
·¢±íÆÀÂÛ
Çë×Ô¾õ×ñÊØ»¥ÁªÍøÏà¹ØµÄÕþ²ß·¨¹æ£¬ÑϽû·¢²¼É«Çé¡¢±©Á¦¡¢·´¶¯µÄÑÔÂÛ¡£
ÆÀ¼Û:
񡀂:
Óû§Ãû: ÑéÖ¤Âë:µã»÷ÎÒ¸ü»»Í¼Æ¬