c#利用HttpWebRequest获取网页源代码,搞了好几天终于解决了,直接获取网站编码进行数据读取,再也不用担心乱码了!

命名空间:Using System.Net

         private static string GetUrlHtml(string url)
{ string strHtml = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse();//从Internet资源返回数据流 if (response.CharacterSet.ToLower() == "gbk")
{
using (Stream respStream = response.GetResponseStream())//读取数据流
{
using (StreamReader str = new StreamReader(respStream, Encoding.GetEncoding("gb2312")))//读取数据
{
strHtml = str.ReadToEnd();
}
}
}
else
{
using (Stream respStream = response.GetResponseStream())//读取数据流
{
using (StreamReader str = new StreamReader(respStream, Encoding.UTF8))//读取数据
{
strHtml = str.ReadToEnd();
}
}
}
return strHtml;
}

c#利用HttpWebRequest获取网页源代码的更多相关文章

  1. 用asp.net c# HttpWebRequest获取网页源代码

    public string GetPage(string url) { HttpWebRequest request = null; HttpWebResponse response = null; ...

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

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

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

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

  4. Java 网络爬虫获取网页源代码原理及实现

    Java 网络爬虫获取网页源代码原理及实现 1.网络爬虫是一个自动提取网页的程序,它为搜索引擎从万维网上下载网页,是搜索引擎的重要组成.传统爬虫从一个或若干初始网页的URL开始,获得初始网页上的URL ...

  5. delphi 获取网页源代码

    //获取网页源代码 var   s: string; begin   s := WebBrowser1.OleObject.document.body.innerHTML; //body内的所有代码 ...

  6. JS远程获取网页源代码的例子

    js代码获取网页源代码. 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < ...

  7. js技术要点---JS 获取网页源代码

    JS 获取网页源代码 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html& ...

  8. C# 获取网页源代码

    /// <summary> /// 获取网页源代码 /// </summary> /// <param name="url"></para ...

  9. NodeJS 获取网页源代码

    获取网页源代码 node 获取网页源代码 var http = require('http'); var url = "http://www.baidu.com/"; // 参数u ...

随机推荐

  1. 让PHP跑在Mac OS X中

    MacBook入手了,配置工作环境,首先得让Mac OS支持PHP.不管你是采用集成的开发环境,比如XAMPP for Mac OS X,还是采用Mac OS中自带的Apache和PHP,甚至自己重新 ...

  2. mysql数据修改-DEDE

    update `dede_arctype` set `templist`='{style}/products.htm' where `templist`='{style}/Product.htm' d ...

  3. LBS配置

    js: <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak= ...

  4. Python 升级

    1.到官网下载对于的版本: 2.下载之后并解压出来,编译: tar xf python.xx.xx.tar.xz sudo mkdir /usr/local/python ./configure -- ...

  5. ZooKeeper笔记--集群安装配置 【转】

    ZooKeeper是一个分布式开源框架,提供了协调分布式应用的基本服务,它向外部应用暴露一组通用服务——分布式同步(Distributed Synchronization).命名服务(Naming S ...

  6. Lintcode--007(不同的子序列)

    题目:http://www.lintcode.com/zh-cn/problem/distinct-subsequences/ 2016-08-25 给出字符串S和字符串T,计算S的不同的子序列中T出 ...

  7. cf B. Berland Bingo

    http://codeforces.com/contest/370/problem/B 题意:给你n个卡片,卡片上有m个不同的数字,这个游戏是随即的从袋子里面抽球,球上有数字1-100:如果第ith玩 ...

  8. MySQL所有函数及操作符

    参考:Function and Operator Reference Name Description ABS() Return the absolute value ACOS() Return th ...

  9. linux下的shell和脚本

    1.各种Unix shell linux下的shell基本是从unix环境中的shell发展而来,贴一下wiki:其中我们常用的,可归类为Bourne Shell(/usr/bin/sh或/bin/s ...

  10. 深入理解linux网络技术内幕读书笔记(二)--关键数据结构

    Table of Contents 1 套接字缓冲区: sk_buff结构 1.1 网络选项及内核结构 1.2 结构说明及操作函数 2 net_device结构 2.1 MTU 2.2 结构说明及操作 ...