asp.net 2.0寮?姝ラ〉?? 2008骞?1??13???????? 23:58
褰??ユ?跺?颁?涓?椤甸?㈣?锋??讹?ASP.NET 浼?浠?涓?涓?绾跨?姹?涓??峰??涓?涓?绾跨?锛?骞跺?椤甸?㈣?锋?????缁?璇ョ嚎绋???涓?涓???????锛?????璇存????姝ョ??椤甸?㈠?ㄨ?锋????翠????ㄧ嚎绋?锛?浠ラ?叉?㈢嚎绋?琚??ㄤ?澶????朵?璇锋???濡?????姝ヨ?锋???涓?I/O 瀵????舵??锛?渚?濡?锛?褰?璇ヨ?锋?璋??ㄤ?涓?杩?绋? Web ???℃???ヨ?㈣?绋??版??搴?骞剁??寰?璋??ㄨ????讹???????缁?瀹???绾跨??ㄨ??ㄨ?????浼?濮?缁?澶?浜??茬疆?舵????杩?绉????典????跺??浼哥缉?э???涓虹嚎绋?姹?涓??????ㄧ嚎绋???????????濡???澶???璇锋???????绾跨??藉??绛?寰? I/O ??浣???瀹??????诲?锛???浼???澶?浣???璇锋?????绛?寰?杩?浜?绾跨??????俱????濂界?????垫???虹?板????????浣?锛???涓洪??瑕?绛?寰??撮?跨??绛?寰????藉???璇锋?????绯?绯??????垫??????琚?濉?婊¤?? ASP.NET ??娉?澶?????缁?璇锋?锛?骞舵??绀?503?????″?ㄤ????ㄢ????璇??? 寮?姝ラ〉?㈢???虹?颁负瑙e??I/O 瀵???????璇锋???瀵艰?寸??姝ょ被??棰???渚?浜?绠?娲????规???椤甸?㈠???瑕??ㄧ嚎绋?姹?涓???涓?涓?绾跨?涓?杩?琛?锛?浣???褰?涓?涓?寮?姝?I/O ??浣???搴??ヨ?? ASP.NET ??淇″?峰苟寮?濮?杩?琛??讹?璇ョ嚎绋?浼?杩?????????绾跨?姹?????浣?瀹?????锛?ASP.NET 浼?浠?绾跨?姹?涓??峰?????涓?绾跨??ュ????澶???璇锋???杩??凤?绾跨?姹???绾跨?浣跨?ㄧ??寰??版??楂?锛???浼哥缉?т???姝ゅ?浠ュ?寮恒???d????ヨ?绛?寰? I/O ??浣?瀹??????诲???绾跨?姝ゆ?跺??浠ョ?ㄤ?澶????朵?璇锋???杩??峰?????存?ュソ澶?灏辨???垮??璇锋??ц????跨?? I/O ??浣?锛???姝ゅ??浠ュ揩??杩??虹?¢????绛?寰?杩??ョ?¢?????堕?磋??夸?瀵规?ょ被璇锋????ц???????寰?澶х??璐??㈠奖???? 浠ヤ???瀛?????锛?http://msdn.microsoft.com/msdnmag/issues/05/10/WickedCode/Default.aspx?loc=zh#S1 涓??㈡??涓?涓??蜂?寮?姝ヨ??ㄧ??绀轰??? <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Async.aspx.cs" Inherits="Async" Async="true" ValidateRequest="false" EnableEventValidation="false" EnableViewState="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> ???? <title>Untitled Page</title> ???? <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> ???? <form id="form1" runat="server"> ???? <div> ???????? <asp:Button ID="btnAnsyc" runat="server" Text="Ansyc" OnClick="btnAnsyc_Click" /> ???????? <asp:Label ID="lblResponse" runat="server" Text=""></asp:Label> ???????? </div> ???? </form> </body> </html> using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net; public partial class Async : System.Web.UI.Page { ???? protected void Page_Load(object sender, EventArgs e) ???? { ???? } ???? protected void btnAnsyc_Click(object sender, EventArgs e) ???? { ???????? Page.AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginRequest), new EndEventHandler(EndRequest)); ???? } ???? IAsyncResult BeginRequest(object sender, EventArgs e, AsyncCallback _callback, object state) ???? { ???????? _request = WebRequest.Create("http://www.baidu.com/index.html"); ???????? return _request.BeginGetResponse(_callback, state); ???? } ???? void EndRequest(IAsyncResult _res) ???? { ???????? string text; ???????? using (WebResponse _Response = _request.EndGetResponse(_res)) ???????? { ???????????? using (System.IO.StreamReader reader = new System.IO.StreamReader(_Response.GetResponseStream(), System.Text.Encoding.Default)) ???????????? { ???????????????? text = reader.ReadToEnd(); ???????????? } ???????? } ???????? lblResponse.Text = text; ???? } ???? WebRequest _request; } |