惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Palo Alto Networks Blog
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Schneier on Security
Engineering at Meta
Engineering at Meta
I
InfoQ
L
LangChain Blog
Cyberwarzone
Cyberwarzone
T
Tenable Blog
WordPress大学
WordPress大学
P
Privacy & Cybersecurity Law Blog
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Jina AI
Jina AI
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Last Watchdog
The Last Watchdog
Last Week in AI
Last Week in AI
Cloudbric
Cloudbric
S
SegmentFault 最新的问题
爱范儿
爱范儿
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
AI
AI
T
Tor Project blog
I
Intezer
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
N
News and Events Feed by Topic
Latest news
Latest news
S
Security Affairs
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog RSS Feed
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
S
Securelist

博客园 - 空军

过多边形边上某点的任意直线等分面积 VisualAPL Installer for Visual Studio 2008 [ZT] Create a Microsoft Access Database Using ADOX and Visual Basic .NET 数的分解,据说是清华的一道复试上机题 为System.Windows.Forms.FontDialog类添加Location属性 f(f(x)) = -x [zt]〖Math〗构造函数使得任意小的区间所对应的值域都是整个实数域 [zt]鸟巢、水立方:同一个地方,同一梦想 〖Math〗据传,任意锐角等于0° 爱因斯坦的超级问题(谁养鱼)SQL解法 “槑囧圐圙”您认得这些汉字吗? MSDN“MidpointRounding 枚举”中文翻译有误 C# 2.0 新特性(泛型、可空类型)应用一例 表达式计算器 Google速记 Google中国编程挑战赛第一轮 Google中国编程挑战赛资格赛 数学家 数据库访问模块
根据URL提取页面的Title,根据网页的charset自动判断Encoding
空军 · 2008-06-08 · via 博客园 - 空军

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

class Program
{
  
// 获取网页的HTML内容,根据网页的charset自动判断Encoding
  static string GetHtml(string url)
  
{
    
return GetHtml(url, null);
  }


  
// 获取网页的HTML内容,指定Encoding
  static string GetHtml(string url, Encoding encoding)
  
{
    
byte[] buf = new WebClient().DownloadData(url);
    
if (encoding != nullreturn encoding.GetString(buf);
    
string html = Encoding.UTF8.GetString(buf);
    encoding 
= GetEncoding(html);
    
if (encoding == null || encoding == Encoding.UTF8) return html;
    
return encoding.GetString(buf);
  }


  
// 根据网页的HTML内容提取网页的Encoding
  static Encoding GetEncoding(string html)
  
{
    
string pattern = @"(?i)\bcharset=(?<charset>[-a-zA-Z_0-9]+)";
    
string charset = Regex.Match(html, pattern).Groups["charset"].Value;
    
try return Encoding.GetEncoding(charset); }
    
catch (ArgumentException) return null; }
  }


  
// 根据网页的HTML内容提取网页的Title
  static string GetTitle(string html)
  
{
    
string pattern = @"(?si)<title(?:\s+(?:""[^""]*""|'[^']*'|[^""'>])*)?>(?<title>.*?)</title>";
    
return Regex.Match(html, pattern).Groups["title"].Value.Trim();
  }


  
// 打印网页的Encoding和Title
  static void PrintEncodingAndTitle(string url)
  
{
    
string html = GetHtml(url);
    Console.WriteLine(
"[{0}] [{1}]", GetEncoding(html), GetTitle(html));
  }


  
// 程序入口
  static void Main()
  
{
    PrintEncodingAndTitle(
"http://www.msdn.net/");
    PrintEncodingAndTitle(
"http://www.cnblogs.com/");
    PrintEncodingAndTitle(
"http://www.cnblogs.com/skyiv/");
    PrintEncodingAndTitle(
"http://www.csdn.net/");
    PrintEncodingAndTitle(
"http://news.163.com/");
  }

}

/* 程序输出:
[] [MSDN: Microsoft Developer Network]
[System.Text.UTF8Encoding] [博客园 - 程序员的网上家园]
[System.Text.UTF8Encoding] [空间/IV - 博客园]
[System.Text.UTF8Encoding] [CSDN.NET - 中国最大的IT技术社区,为IT专业技术人员提供最全面的信息传播和服务平台]
[System.Text.DBCSCodePageEncoding] [新闻中心_网易新闻]
*/