`
leonardleonard
  • 浏览: 772776 次
社区版块
存档分类
最新评论

利用WebClient和WebRequest类获得网页源代码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();
}
}
}
分享到:
评论

相关推荐

    c#获取网页源码案例

    亲测,成功运行。 c#获取网页源码案例,C#获取指定网页HTML原代码可使用 WebClient WebRequest HttpWebRequest 三种方式来实现。 当然也可使用webBrowse!在此就不研究webBrowse如何获取了。

    Webbrowser调用dll,简单方便获取request response header等资源的例子,实现了HttpWebResquest等实现的功能

    老外写的Webbrowser调用dll,简单方便获取request header,response header等各种网络资源的例子,实现了原来只有WebClient,WebRequest和HttpWebResquest才能实现的功能。 需要dll的完整源代码见我上传的另外一个资源...

    C#全能速查宝典

    分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用的属性、方法、类和各种技术,...

    Visual C# 2005程序设计自学手册 随书源码第一部分(共三部)

    光盘提供了书中所有实例的源代码,全部源代码都经过精心调试,在Windows XP/Windows 2000/Windows 2003 Server下全部通过,保证能够正常运行。  本书适用于C#初、中级用户,也可作为大、中专院校师生和培训班的教材...

    c# http post get

    //获取返回的网页源代码 client.DownloadFile("http://www.codepub.com/upload/163album.rar",@"C:\163album.rar");//下载文件 client.OpenRead("http://passport.baidu.com/?login","username=zhangsan&password=...

Global site tag (gtag.js) - Google Analytics