使用.NET读取远程网页的源代码

时间:2007/1/27 11:38:11      阅读:4395          
利用.NET框架提供的 WebClient类 和 WebRequest类,我们可以很轻易地得到给定URL地址的源代码。运行效果:

源代码:
GetPage.aspx:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="GetPage.aspx.vb" 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>得到任意网页源代码——www.web3.cn</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center" style="FONT-WEIGHT: bold">得到任意网页源代码</div>
<asp:TextBox id="UrlText" runat="server" Width="400px">http://www.web3.cn</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>

GetPage.aspx.vb:
Imports System.io
Imports System.Net
Partial Class _GetPage
Inherits System.Web.UI.Page
Private PageUrl As String = ""
Protected Sub WebClientButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebClientButton.Click
PageUrl
= UrlText.Text
Dim wc As Net.WebClient = New Net.WebClient()
wc.Credentials
= CredentialCache.DefaultCredentials

'/方法一:
Dim pageData() As Byte = 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()
End Sub

Protected Sub WebRequestButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebRequestButton.Click
PageUrl
= UrlText.Text
Dim request As WebRequest = WebRequest.Create(PageUrl)
Dim response As WebResponse = request.GetResponse()
Dim resStream As Stream = response.GetResponseStream()
Dim sr As StreamReader = New StreamReader(resStream, System.Text.Encoding.Default)
ContentHtml.Text
= sr.ReadToEnd()
resStream.Close()
sr.Close()
End Sub
End Class

注:如在获取代码时出现安全提示,请在web.config文件中加入以下一行即可
1<pages buffer="true" validateRequest="false">
评论
  • Re:使用.NET读取远程网页的源代码  (2007/3/15 20:30:04) by cyxxde 
    很不错,受教了。
  • Re:使用.NET读取远程网页的源代码  (2007/3/31 12:09:06) by fenghaoda 
    可以的。谢谢
  • Re:使用.NET读取远程网页的源代码  (2007/8/5 9:57:06) by Hins 
    <pages buffer="true" validateRequest="false">的具体位置在哪里?怎么报错了?
标 题:
 
姓 名:
 
主 页:

验证码:

评论:
 

Because of the cache,you may see your comments several minutes later.