天天看點

asp.net 2.0異步頁面

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;

}

繼續閱讀