`

利用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#获取网页源代码的5种方法

    `WebClient` 类提供了下载数据的便捷方法,可以直接获取到网页的源代码。 ```csharp using System.Text; using System.Net; private string GetHtml(string url) { WebClient myWebClient = new WebClient(); ...

    c#获取网页源码案例

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

    C#爬虫代码和文档,爬京东

    `c#利用WebClient和WebRequest获取网页源代码的比较.docx`文档可能详细对比了这两个类的优缺点以及在爬虫项目中的具体使用场景。 5. **关于.txt**:这是一个文本文件,通常用于提供项目的基本信息、作者、许可证等...

    C#网络应用编程第二版附代码

    提供的源代码可以帮助读者深入理解书中的示例,动手实践并调试代码,巩固理论知识。 总的来说,《C#网络应用编程第二版》是C#开发者进阶网络编程领域的宝贵资料,通过阅读和实践,你将能够熟练地运用C#开发出功能...

    C#—get返回网页源码

    根据给定的文件信息,本篇文章将详细介绍如何使用C#语言及.NET框架通过GET方法获取网页的源代码。本文将涵盖以下知识点: 1. **基础知识介绍**:介绍C#与.NET的基本概念及其在Web开发中的应用。 2. **HTTP请求简介*...

    获取网页中某一部分内容的源代码

    在C#中,通常使用`WebClient`类或`HttpClient`类来下载网页的内容。`WebClient`是一个非常简单易用的类,可以用来下载网页的HTML源码。例如,在提供的代码片段中,`getHtml`方法就是利用`WebClient`来实现的。 ####...

    c#网络编程实例源代码

    本资源提供了"C#网络编程实例源代码",涵盖了异步套接字服务器端、异步套接字客户端、文件传输服务器端、文件传输客户端以及一个浏览器的实现,这些都是网络编程中的典型应用场景。 1. **异步套接字服务器端**: ...

    京东商品评论数量爬虫源码

    最后,压缩包中的"PaChong"可能包含了这个C#爬虫项目的源代码文件。分析这些源代码可以帮助我们更好地理解上述过程的具体实现,包括如何处理HTTP请求、解析HTML、以及如何组织和存储数据等。如果你想要学习或改进这...

    C#下载模拟

    `WebClient`类提供了简单的下载和上传文件的方法,如`DownloadFile`、`DownloadData`等,它们可以直接将远程文件保存到本地或者获取文件内容。 ```csharp using System.Net; WebClient client = new WebClient(); ...

    c#.net实例+100个精彩实例

    实例可能涵盖Socket编程、WebClient和WebRequest类的使用。 8. **事件和委托**:C#中的事件和委托是实现组件之间通信的重要机制。实例可能包含用户界面事件处理或自定义事件的示例。 9. **LINQ(Language ...

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

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

    C#全能速查宝典

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

    ftp.zip_c# ftp工具_c#中ftp开发_c#开发ftp_ftp上传下载_文件管理

    这个"ftp.zip"压缩包中可能包含一个C#编写的FTP工具源代码,实现了文件上传、下载和文件夹管理的功能。开发者可以参考这些代码来学习如何在C#环境中使用FTP协议进行文件管理。了解并熟练掌握FTP的使用,对于开发涉及...

    c#网路编程指导ppt

    1. **WebClient**: 这是一个简单的网络访问类,支持下载和上传文件,以及获取网页内容。例如,你可以使用DownloadString方法获取HTML源代码,或使用UploadString方法向服务器发送POST请求。 ```csharp WebClient ...

    C# send post&get

    // 显示页面源代码 ``` 3. **下载文件:** ```csharp client.DownloadFile("http://www.codepub.com/upload/163album.rar", @"C:\163album.rar"); ``` 4. **发送带参数的 POST 请求:** ```csharp string ...

    C# 2005 入门经典 全部源码

    - WebClient和WebRequest类的使用。 27. **Chapter28 - 设计模式** - 常见设计模式的介绍,如工厂模式、单例模式、观察者模式。 28. **Chapter29 - 测试和调试** - 单元测试的原理,使用NUnit或MSTest框架。 -...

    使用C# Winform应用程序获取网页源文件的解决方法

    例如,如果只需要简单地获取网页源代码,WebClient可能是最好的选择;如果需要处理复杂的HTTP头部或者执行更复杂的操作,HttpWebRequest可能更适合。无论哪种方式,都需要注意异常处理,以确保程序的健壮性。在保存...

    Url2Image.rar

    在C#中,可以使用HttpWebRequest或HttpClient类来发起HTTP请求,获取网页的HTML源代码。例如,我们可以创建一个HttpWebRequest对象,设置其URL属性为待转换的网页地址,然后通过GetResponse方法获取响应,再通过...

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

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

Global site tag (gtag.js) - Google Analytics