asynchronous page downloading : WebClient « Development « ASP.NET Tutorial






<%@ Page Async="true" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
    Inherits="Default" Trace="true" %>

<!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>Async pages</title>
</head>

<body>
    <form id="form1" runat="server">
        <div id="pageContent">
        
            <% = rssData %>
        
        </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Xml;
using System.Net;
using System.IO;
using System.Text;
using System.Xml.XPath;


public partial class Default : System.Web.UI.Page
{
    const string RSSFEED = "http://weblogs.asp.net/despos/rss.aspx";
    private WebRequest req;
    public string rssData;

    protected void Page_Load(object sender, EventArgs e)
    {
        AddOnPreRenderCompleteAsync(
            new BeginEventHandler(BeginTask),
            new EndEventHandler(EndTask));
    }

    IAsyncResult BeginTask(object sender, EventArgs e, AsyncCallback cb, object state)
    {
        Trace.Warn("Begin async: Thread=" + 
                    Thread.CurrentThread.ManagedThreadId.ToString());

        req = WebRequest.Create(RSSFEED);

        return req.BeginGetResponse(cb, state);
    }

    void EndTask(IAsyncResult ar)
    {
        string text;
        using (WebResponse response = req.EndGetResponse(ar))
        {
            StreamReader reader;
            using (reader = new StreamReader(response.GetResponseStream()))
            {
                text = reader.ReadToEnd();
            }

            rssData = ProcessFeed(text);
        }
        Trace.Warn("End async: Thread=" + 
                    Thread.CurrentThread.ManagedThreadId.ToString());
        
    }

    string ProcessFeed(string feed)
    {
        StringBuilder sb = new StringBuilder();

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(feed);

        XPathNavigator nav = doc.CreateNavigator();
        XPathNodeIterator iterator;

        iterator = nav.Select("/rss/channel/title");
        iterator.MoveNext();
        sb.AppendFormat("<h3>{0}</h3>", iterator.Current.Value);
        iterator = nav.Select("/rss/channel/description");
        iterator.MoveNext();
        sb.AppendFormat("<i>{0}</i>", iterator.Current.Value);

        XmlNodeList items = doc.SelectNodes("/rss/channel/item");
        sb.Append("<ul>");
        foreach (XmlNode n in items)
        {
            XmlNode title = n.SelectSingleNode("title");
            XmlNode link = n.SelectSingleNode("link");
            XmlNode date = n.SelectSingleNode("pubDate");
            sb.AppendFormat("<li><a href=\"{0}\">{1}</a> on {2}</li>", link.InnerText, title.InnerText, date.InnerText);
        }
        sb.Append("</ul>");

        return sb.ToString();
    }
}








9.46.WebClient
9.46.1.Trigger Batch Compile
9.46.2.Read page header
9.46.3.asynchronous page downloading
9.46.4.Retrieve web page