ASP.NET Tutorial/Development/WebClient

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

asynchronous page downloading

<%@ 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();
    }
}


Read page header

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ScrapeHeadings" %>
<!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>Scraping Headings</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:Panel ID="panUrl" runat="server" GroupingText="Search" CssClass="myPanel">
         Enter Url:
         <asp:TextBox ID="txtUrl" runat="server" Columns="50"></asp:TextBox><br />
         <asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search" />
      </asp:Panel>
      
      
      <asp:Panel ID="panHeadings" 
                 runat="server" 
                 GroupingText="Headings in this Url" 
                 CssClass="myPanel" >
         <asp:Literal ID="litContent" runat="server" />
      </asp:Panel>
    </form>
</body>
</html>

File: Default.aspx.cs
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;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
public partial class ScrapeHeadings : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
     litContent.Text = "";
   }
   protected void btnSearch_Click(object sender, EventArgs e)
   {
      try
      {
         WebClient client = new WebClient();
         string content = client.DownloadString(txtUrl.Text);
         Regex reg = new Regex(@"<h\d>.+</h\d>", RegexOptions.IgnoreCase);
         MatchCollection mc = reg.Matches(content);
         foreach (Match m in mc)
         {
            litContent.Text += HttpUtility.HtmlEncode(m.Value) + "<br/>";
         }
      }
      catch
      {
         litContent.Text = "Could not connect to " + txtUrl.Text;
      }      
   }
}


Retrieve web page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="GetBookImage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
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.Text.RegularExpressions;
using System.Net;
using System.IO;
public partial class GetBookImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    FindBook findBook = new FindBook();
    Response.Redirect(findBook.GetImageUrl(Request.QueryString["isbn"]));
    }
}
public class FindBook
{
  public string GetImageUrl(string isbn)
  {
    try
    {
      isbn = isbn.Replace("-", "");
      string bookUrl = "http://www.amazon.ru/exec/obidos/ASIN/" + isbn;
      string bookHtml = GetWebPageAsString(bookUrl);
      string imgTagPattern = "<img src=\"(http://images.amazon.ru/images/P/" + isbn + "[^\"]+)\"";
      Match imgTagMatch = Regex.Match(bookHtml, imgTagPattern);
      return imgTagMatch.Groups[1].Value;
    }
    catch
    {
      return "";
    }
  }
  public string GetWebPageAsString(string url)
  {
    WebRequest requestHtml = WebRequest.Create(url);
    WebResponse responseHtml = requestHtml.GetResponse();
    StreamReader r = new StreamReader(responseHtml.GetResponseStream());
    string htmlContent = r.ReadToEnd();
    r.Close();
    return htmlContent;
  }
}


Trigger Batch Compile

using System;
using System.Net;
using System.IO;
using System.Text;
class TriggerBatchCompile {
private static string[] urls = {
"http://localhost/page.aspx",
"http://localhost/anotherApplication/page.aspx",
"http://localhost/yetAnotherApplication/page.aspx"
};
    public static void Main(string[] args) {
        WebResponse result = null;
        for(int i = 0; i < urls.Length; i++) {
            try {
                Console.WriteLine(urls[i]);
                WebRequest request = WebRequest.Create(urls[i]);
                result = request.GetResponse();
                Stream receiveStream = result.GetResponseStream();
                StreamReader streamReader = new StreamReader(receiveStream);
                char[] read = new Char[256];
                int count = streamReader.Read( read, 0, 256 );
                while (count > 0) {
                    count = streamReader.Read(read, 0, 256);
                }
            }
            catch(Exception e) {
                Console.WriteLine(e);
            } 
            finally {
                if ( result != null ) {
                    result.Close();
                }
            }
        }
    }
}