利用WebClient和WebRequest類獲得網頁源代碼 - .net語言 -

利用WebClient和WebRequest類獲得網頁源代碼

時間:2010-01-30 09:27:16   來源:   評論:加載中...   點擊:加載中...
利用.net框架提供的 WebClient類 和 WebRequest類,我們可以很輕易地得到給定URL地址的源代碼,很簡單,以下是C#的完整的例子.查看例子GetP...

利用.net框架提供的 WebClient類 和 WebRequest類,我們可以很輕易地得到給定URL地址的源代碼,很簡單,以下是C#的完整的例子.

查看例子

GetPageHtml.ASPx

<%@ Page language="c#" validateRequest = "false" Codebehind="GetPageHtml.aspx.cs"

AutoEventWireup="false" Inherits="eMeng.Exam.GetPageHtml" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

<HEAD>

<title>得到網頁源代碼</title>

<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">

<meta name="CODE_LANGUAGE" Content="C#">

<meta name="vs_defaultClientScript" content="JavaScript">

<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

</HEAD>

<body MS_POSITIONING="GridLayout">

<form id="aspNetBuffer" method="post" runat="server">

<div align="center" style="FONT-WEIGHT: bold">得到任意網頁源代碼</div>

<asp:TextBox id="UrlText" runat="server" Width="400px">http://dotnet.aspx.cc/content.aspx

</asp:TextBox>

<asp:Button id="WebClientButton" Runat="server" Text="用WebClient得到"></asp:Button>

<asp:Button id="WebRequestButton" runat="server" Text="用WebRequest得到"></asp:Button>

<br>

<asp:TextBox id="ContentHtml" runat="server" Width="100%" Height="360px" TextMode="MultiLine">

</asp:TextBox>

</form>

</body>

</HTML>
 

 GetPageHtml.aspx.cs

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Net;

using System.Text;

 namespace eMeng.Exam

{

/// <summary>

/// GetPageHtml 的摘要說明。

/// </summary>

public class GetPageHtml : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Button WebClientButton;

protected System.Web.UI.WebControls.Button WebRequestButton;

protected System.Web.UI.WebControls.TextBox ContentHtml;

protected System.Web.UI.WebControls.TextBox UrlText;

private string PageUrl = "";

private void Page_Load(object sender, System.EventArgs e)

{}

 #region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

InitializeComponent();

base.OnInit(e);

}

 /// <summary>

/// 設計器支持所需的方法 - 不要使用代碼編輯器修改

/// 此方法的內容。

/// </summary>

private void InitializeComponent()

{

this.WebClientButton.Click += new System.EventHandler(this.WebClientButton_Click);

this.WebRequestButton.Click += new System.EventHandler(this.WebRequestButton_Click);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void WebClientButton_Click(object sender, System.EventArgs e)

{

PageUrl = UrlText.Text;

WebClient wc = new WebClient();

wc.Credentials = CredentialCache.DefaultCredentials;

 
///方法一:

Byte[] pageData = wc.DownloadData(PageUrl);

ContentHtml.Text = Encoding.Default.GetString(pageData);
 

/// 方法二:

/// ***************代碼開始**********

/// Stream resStream = wc.OpenRead(PageUrl);

/// StreamReader sr = new StreamReader(resStream,System.Text.Encoding.Default);

/// ContentHtml.Text = sr.ReadToEnd();

/// resStream.Close();

/// **************代碼結束********

///

wc.Dispose();

}

 private void WebRequestButton_Click(object sender, System.EventArgs e)

{

PageUrl=UrlText.Text;

WebRequest request = WebRequest.Create(PageUrl);

WebResponse response = request.GetResponse();

Stream resStream = response.GetResponseStream();

StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);

ContentHtml.Text = sr.ReadToEnd();

resStream.Close();

sr.Close();

}

}

}



相關熱詞搜索:

 
上一篇:.NET中數據列表控件的通用屬性
下一篇:ASP.NET特殊字符串替換函數
收藏 將此文推薦給朋友
分享到:
10个数复式三中三多少组公式