HTML:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 id="Head1" runat="server">
<title>得到网页源代码</title>
</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得到" OnClick="WebClientButton_Click"></asp:Button>
<asp:Button id="WebRequestButton" runat="server" Text="用WebRequest得到" OnClick="WebRequestButton_Click"></asp:Button> <asp:TextBox id="ContentHtml" runat="server" Width="100%" Height="360px" TextMode="MultiLine">
</asp:TextBox>
</form>
</body>
</html>

ASPX:

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;
public partial class _Default : System.Web.UI.Page
{
private string PageUrl = "";
protected void Page_Load(object sender, EventArgs e)
{ }
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); /// 方法二:
/// ***************代码开始**********
/// Stream resStream = wc.OpenRead(PageUrl);
/// StreamReader sr = new StreamReader(resStream,System.Text.Encoding.Default);
/// ContentHtml.Text = sr.ReadToEnd();
/// resStream.Close();
/// **************代码结束********
///
wc.Dispose();
}
protected void WebRequestButton_Click(object sender, 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(); }
}

WebClient和WebRequest获取html代码的更多相关文章

  1. c#利用WebClient和WebRequest获取网页源代码的比较

    前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...

  2. c#利用WebClient和WebRequest获取网页源代码

    C#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现.   WebClient类获取网页源代码   WebClient类   WebClient ...

  3. 第三节:总结.Net下后端的几种请求方式(WebClient、WebRequest、HttpClient)

    一. 前言 前端调用有Form表单提交,ajax提交,ajax一般是用Jquery的简化写法,在这里不再过多介绍: 后端调用大约有这些:WebCient.WebRequest.Httpclient.W ...

  4. WebRequest 获取网页乱码

    问题:在用WebRequest获取网页源码时得到的源码是乱码. 原因:1,编码不对 解决办法:设置对应编码 WebRequest request = WebRequest.Create(Url);We ...

  5. [PHP] debug_backtrace()可以获取到代码的调用路径追踪

    查看代码的时候,看到有使用这个函数,测试一下 1.debug_backtrace()可以获取到代码的调用追踪,以数组形式返回 2.debug_print_backtrace() — 打印一条回溯,直接 ...

  6. Eclipse已经安装了SVN插件,但是在获取SVN代码时,一直处于progress....

    Eclipse已经安装了SVN插件,但是在获取SVN代码时,一直处于progress.... 后来升级把SVN插件升级到了1.10x,在获取就看轻轻松松搞定了 由此得出: 在安装EclipseSVSN ...

  7. Csharp:WebClient and WebRequest use http download file

    //Csharp:WebClient and WebRequest use http download file //20140318 塗聚文收錄 string filePath = "20 ...

  8. discuz开发实现自动获取后台入口代码

    一般discuz后台入口默认是admin.php,不过部分用户为了安全可能会修改后台入口文件名称,可以用代码 '.ADMINSCRIPT.'?frame=no&action=tools& ...

  9. C#使用正则表达式获取HTML代码中a标签里包含指定后缀的href的值

    //C#使用正则表达式获取HTML代码中a标签里包含指定后缀的href的值,表达式如下: Regex regImg = new Regex(@"(?is)<a[^>]*?href ...

随机推荐

  1. selenium chrome 自动加载flash

    #coding:utf-8from selenium import webdriverfrom selenium.webdriver.support.select import Selectfrom ...

  2. 女性睾酮水平高(High Testosterone Levels in Women)

    在外国网站了解睾酮高的一些资料,顺便记录生活点滴. 摘自网址:https://www.healthline.com/health/high-testosterone-in-women 高睾酮的妇女 睾 ...

  3. 安卓——implements——OnClickListener

    //实现底部的按钮激活监听 设置不同src class click implements OnClickListener{ @Override public void onClick(View v) ...

  4. CRM 价格批导2<上一个太多冗余>

    INCLUDE:LCRM_MKTPL_COND_IFF39 *--------------------------------------------------------------------- ...

  5. Windows下安装 Memcache

    1.下载Memcached Windows服务端程序.(memcached >= 1.4.5 版本安装32 32位系统 1.4.5版本:http://static.runoob.com/down ...

  6. CentOS最小安装无法使用ifconfig命令

    问题描述: 1.先解决联网问题,详情查看:http://www.cnblogs.com/zhi-leaf/p/5983470.html. 2.执行net-tools,执行命令 yum install ...

  7. chrome google mozilla firefox bookmarks import export

    chrome导出导入bookmarks 1◆ google帐号 自己申请,脑补   2◆ google访问 脑补   suggestion   Lantern   3◆ 步骤 4◆ 导入 sample ...

  8. day_07_python_1124

    01 昨日内容回顾 数据类型补充: str <---> list split join list <---> set set(list) list(set()) list &l ...

  9. day26 第二阶段共享

    第二阶段共享--网络编程 一.C/S架构 : 客户端(client)/服务端(sever)框架 B/S架构: 浏览器(brower)/服务端(sever)架构 软件CS架构: 服务端连接到浏览器,QQ ...

  10. 非图片格式如何转成lmdb格式--caffe

    链接 LMDB is the database of choice when using Caffe with large datasets. This is a tutorial of how to ...