WebClient和WebRequest获取html代码

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代码,布布扣,bubuko.com

时间: 2024-08-04 07:24:14

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

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

前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取网页源代码 WebClient类 WebClient类位于System.Net命名空间下,WebClient类提供向URI标识的任何本地.Intranet或Internet资源发送数据以及从这些资源接收数据的公共方法. 源代码 ///引用命名空间using System.IO;using Syste

WebRequest 获取网页乱码

问题:在用WebRequest获取网页源码时得到的源码是乱码. 原因:1,编码不对 解决办法:设置对应编码 WebRequest request = WebRequest.Create(Url);WebResponse response = await request.GetResponseAsync(); Stream stream = response.GetResponseStream();StreamReader reader = new StreamReader(stream, Enc

淘宝开放平台使用WebClient,WebRequest访问时的错误提示导致麻烦

淘宝开放平台(TOP)提供OAuth2.0支持 通过C#的WebClient/WebRequest直接访问时会提示grant type is empty,这是一个非常恼人的错误,你会发现即使传了这个参数提示依然是这样. 使用linux的curl不会有这样的问题. 通过多次排查,对比,将近8小时我才找到问题是Content-Type必须为application/x-www-form-urlencoded. 淘宝开放平台使用WebClient,WebRequest访问时的错误提示导致麻烦

java解析XML获取城市代码

运行前先导入dom4j架包,由于我们公司用的代理服务器所以下面我设置了代理ip,不需要的可直接忽略 package com.chengshidaima.tools; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.net.URLConn

远程仓库获取最新代码合并到本地分支

这里共展示两类三种方式. 1.git pull:获取最新代码到本地,并自动合并到当前分支 命令展示 //查询当前远程的版本 $ git remote -v //直接拉取并合并最新代码 $ git pull origin master [示例1:拉取远端origin/master分支并合并到当前分支] $ git pull origin dev [示例2:拉取远端origin/dev分支并合并到当前分支] 分析:不推荐这种方式,因为是直接合并,无法提前处理冲突. 2.git fetch + mer

获取城市代码

http://m.weather.com.cn/data5/city.xml 返回所有省/直辖市的编号 01|北京,02|上海,03|天津,04|重庆,05|黑龙江,06|吉林,07|辽宁,08|内蒙古,09|河北,10|山西,11|陕西,12|山东,13|新疆,14|西藏,15|青海,16|甘肃,17|宁夏,18|河南,19|江苏,20|湖北,21|浙江,22|安徽,23|福建,24|江西,25|湖南,26|贵州,27|四川,28|广东,29|云南,30|广西,31|海南,32|香港,33|澳

Ajax 获取数据代码

无刷新获取字符串: Html网页中: <script> //定义异步对象 var xmlHttp; //封装方法 function CreateXMLHTTP() {   try {            xmlHttp = new XMLHttpRequest();        } catch (e) {  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");      } } window.onload = functio

Gson源码分析(贰) 类型获取和代码规范

我们使用Gson的时候基本是需要先定义一个数据模型,然后通过一个String流转化为我们OO的对象.那么对于一个框架来说,如何去获得用户想要的数据类型呢?并且我们又要如何通过这种既定的类型来构造出我们需要的对象?或许你的第一反应就是传递一个Clazz进去,然后通过反射的方法来获得我们的实际对象.跟着这个想法我们来实验一下: public <T>T createObject(Class<T> clazz) { try { return (T)clazz.newInstance();

使用innerHTML获取HTML代码时,HTML标记属性的双引号好多都消失不见了,原来是属性值中包含空格才会保留双引号

最近搞的一个项目中所使用的方式比较奇怪,用Label显示HTML内容,然后不断地使用JS把Label的innerHTML复制到TextBox中. 但是,昨天发现了一个问题,获取元素值的时候,有时候正常,有时候不正常,然后进入了漫长的DEBUG.. 调试过程中,监测Label和TextBox的值,发现获取到Label的值是未被改变的,控件加载的时候是怎样就怎样,在页面中是以SPAN元素显示,在查看网页源代码的时候可以看到被修改了,但是在后台无论怎么获取都是页面加载时赋予的未经修改之前的值. 而Te