Skip to content

Instantly share code, notes, and snippets.

@yangg
Created May 25, 2012 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangg/2786430 to your computer and use it in GitHub Desktop.
Save yangg/2786430 to your computer and use it in GitHub Desktop.
ViewSource
using System;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
class Program
{
static void Main()
{
ViewSource("http://www.baidu.com/");
ViewSource("http://www.google.com/");
ViewSource("http://www.163.com/");
}
static String ViewSource(string url)
{
string source = string.Empty, charset = string.Empty;
HttpWebRequest httpreq = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse httpresp = (HttpWebResponse)httpreq.GetResponse())
{
using (MemoryStream stream = new MemoryStream())
{
httpresp.GetResponseStream().CopyTo(stream);
charset = httpresp.CharacterSet;
if (string.IsNullOrEmpty(charset) || charset == "ISO-8859-1")
{
source = Encoding.GetEncoding("ISO-8859-1").GetString(stream.GetBuffer(), 0, (int)stream.Length);
stream.Position = 0;
if (Regex.IsMatch(source, @"^(?:[\x00-\x7f]|[\xfc-\xff][\x80-\xbf]{5}|[\xf8-\xfb][\x80-\xbf]{4}|[\xf0-\xf7][\x80-\xbf]{3}|[\xe0-\xef][\x80-\xbf]{2}|[\xc0-\xdf][\x80-\xbf])+$"))
{
charset = "utf-8";
}
else
{
charset = "gb2312";
}
}
Console.WriteLine(httpresp.CharacterSet + " => " + charset);
source = Encoding.GetEncoding(charset).GetString(stream.GetBuffer(), 0, (int)stream.Length);
}
}
Console.WriteLine(source.Substring(0, source.Length > 500 ? 500 : source.Length));
return source;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment