日期:2014-04-30  浏览次数:23083 次

利用.NET的 WebClient类 和 WebRequest类,我们可以很容易地得到给定URL地址的源代码。次要代码如下:

GetPage.aspx:
 

程序代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetPage.aspx.cs" Inherits="GetPage" %>
<!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>获取任意网页HTML代码</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align:center;">
        <asp:TextBox ID="UrlText" runat="server" Width="400" Text="http://www.gsdesign.cn"></asp:TextBox>
        <asp:Button ID="WebClientButton" runat="server" Text="用WebClient得到" OnClick="WebClientButton_Click" />
        <asp:Button ID="WebRequestButton" runat="server" Text="用WebRequest得到" OnClick="WebRequestButton_Click" />
        <br />
        <asp:TextBox ID="ContentHtml" runat="server" Width="100%" Height="360" TextMode="MultiLine"></asp:TextBox>  
    </div>
    </form>
</body>
</html>
 



GetPage.aspx.cs:
 

程序代码

using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Net;

public partial class GetPage : System.Web.UI.Page
{
    private string pageUrl = "";

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 使用WebClient方法
    /// </summary>
    protected void WebClientButton_Click(object sender, EventArgs e)
    {
        pageUrl = UrlText.Text;
        WebClient wc = new WebClient();
        wc.Credentials = CredentialCache.DefaultCredentials;

        //方法一
        byte[] pageData = wc.DownloadData(pageUrl);
        ContentHtml.Text = Encoding.Default.GetString(pageData);