public class WebDownload : WebClient
{
private int _timeout;
/// <summary>
/// 超时时间(毫秒)
/// </summary>
public int Timeout
{
get
{
return _timeout;
}
set
{
_timeout = value;
}
} public WebDownload()
{
this._timeout = ;
} public WebDownload(int timeout)
{
this._timeout = timeout;
} protected override WebRequest GetWebRequest(Uri address)
{
var result = base.GetWebRequest(address);
result.Timeout = this._timeout;
return result;
} public string Get(string url)
{
string html = "";
var data = this.DownloadData(url);
var r_utf8 = new System.IO.StreamReader(new System.IO.MemoryStream(data), Encoding.UTF8); //将html放到utf8编码的StreamReader内
var r_gbk = new System.IO.StreamReader(new System.IO.MemoryStream(data), Encoding.Default); //将html放到gbk编码的StreamReader内
var t_utf8 = r_utf8.ReadToEnd(); //读出html内容
var t_gbk = r_gbk.ReadToEnd(); //读出html内容
if (!isLuan(t_utf8)) //判断utf8是否有乱码
{
html = t_utf8;
}
else
{
html = t_gbk;
} return html;
} bool isLuan(string txt)
{
var bytes = Encoding.UTF8.GetBytes(txt);
//239 191 189
for (var i = ; i < bytes.Length; i++)
{
if (i < bytes.Length - )
if (bytes[i] == && bytes[i + ] == && bytes[i + ] == )
{
return true;
}
}
return false;
} }

黄聪:C#中WebClient自动判断编码是UTF-8还是GBK,并且有超时判断功能的更多相关文章

  1. 黄聪:VPS实现自动定时备份网站数据以及Mysql数据库到百度云同步盘

    建站多了,备份成了头疼的问题,因为你不知道你的VPS什么时候会宕机或者服务商跑路,一旦网站数据丢失,那么相当于前功尽弃了,所以自己研究出了一套自动备份的方法. 需要的东西: 1.一个VPS(虚拟空间没 ...

  2. 黄聪:win7 QQ自动远程协助 提示关闭了远程桌面

    最近在使用QQ自动远程协助的时候,输入完远程验证密码后,提示“关闭了远程桌面” 系统环境:win7 64位 问题描述:在使用QQ自动远程协助,对方QQ提示关闭了远程桌面. 解决办法:将2台电脑的时间调 ...

  3. 解决:IE中不能自动选择UTF-8编码的解决方法

    IE中不能自动选择UTF-8编码的解决办法 在windows操作系统上使用IE作为浏览器时.常常会发生这样的问题:在浏览使用UTF-8编码的网页时,浏览器无法自动侦测(即没有设定“自动选择”编码格式时 ...

  4. C# -- WebClient自动获取web页面编码并转换

    C# -- WebClient自动获取web页面编码并转换 抽个时间,写篇小文章,最近有个朋友,用vb开发一个工具,遇到WebClient获取的内容出现乱码,可惜对vb不是很熟悉,看了几分钟vb的语法 ...

  5. 黄聪:VS2010开发如何在c#中使用Ctrl、Alt、Tab等全局组合快捷键

    1.新建一个类 HotkeyHelper  using System; using System.Runtime.InteropServices; using System.Windows.Forms ...

  6. 黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block 代理对象(Proxy Object) ...

  7. 【转】黄聪:HtmlAgilityPack教程案例

    [转]黄聪:HtmlAgilityPack教程案例 HtmlAgilityPack中的HtmlNode类与XmlNode类差不多,提供的功能也大同小异.下面来看看该类提供功能. 一.静态属性 publ ...

  8. 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ...

  9. 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block 企业库日志应用程序模块工作原理图:   从上图我们可以 ...

随机推荐

  1. Oracle数据库Linux下的导入IMP

    和相关篇的EXP相对应的用了如下的导入方法. [oracle@localhost ~]$ imp Import: Release 11.2.0.1.0 - Production on Fri Sep ...

  2. android中ListView控件&&onItemClick事件中获取listView传递的数据

    http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...

  3. leetcode 127. Word Ladder ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  4. leetcode 112 Path Sum ----- java

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  5. URAL 1320 Graph Decomposition(并查集)

    1320. Graph Decomposition Time limit: 0.5 secondMemory limit: 64 MB There is a simple graph with an ...

  6. Android sdk 镜像服务器资源

    大连东软信息学院镜像服务器地址:- http://mirrors.neusoft.edu.cn 端口:80北京化工大学镜像服务器地址:- IPv4: http://ubuntu.buct.edu.cn ...

  7. AssetStore资源名字

    NGUI A Pathfinding Project Pro PlayMaker 2DToolkit Scene Manager UniLOD UniLUA Save Game-JSON+Binary ...

  8. 此博客记录我的进阶之路(PHP、C、Python、Erlang)

    强大自己!赚钱,装修,娶媳妇!!

  9. jquery ajax POST 例子详解

    function test(){ $.ajax({ //提交数据的类型 POST GET type:"POST", //提交的网址 url:"testLogin.aspx ...

  10. Avoiding PostgreSQL database corruption

    TL;DR: Don't ever set fsync=off, don't kill -9 the postmaster then deletepostmaster.pid, don't run P ...