[资料整理]获取页面编码的方法

1,通过分析Header提取编码。

WebRequest webRequest = WebRequest.Create(url);
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
Regex reg_charset = new Regex(@"charset\b\s*=\s*(?<charset>[^""]*)") ;
WebHeaderCollection headers = webResponse.Headers;
string encodingName = string.Empty;
string contentType = headers["Content-Type"];
if (contentType.IndexOf("charset") > 0 && reg_charset.IsMatch(ContentType))
{
       encodingName = reg_charset.Match(contentType).Groups["charset"].Value;
} 

2.通过分析BOM(Byte Order Mark)提取编码

             string encodingName = string.Empty;
             WebRequest webRequest = WebRequest.Create(url);
             HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
             Stream stream = webResponse.GetResponseStream();
             byte[] htmlByte = GetByteContent(stream);
             string codingName = string.Empty;
             Encoding encoding = Encoding.UTF8;
             byte[] bomByte = encoding.GetPreamble();
             if (htmlByte.Length > bomByte.Length && htmlByte[0] == bomByte[0] &&
                 htmlByte[1] == bomByte[1] && htmlByte[2] == bomByte[2])
             {
                 codingName = "utf-8";
             }
             encoding = Encoding.Unicode;
             bomByte = encoding.GetPreamble();
             if (codingName == string.Empty && htmlByte.Length > bomByte.Length &&
                 htmlByte[0] == bomByte[0] && htmlByte[1] == bomByte[1])
             {
                 codingName = "unicode";
             }
             encoding = Encoding.UTF32 ;
             bomByte = encoding.GetPreamble();
             if (codingName == string.Empty &&  htmlByte.Length > bomByte.Length &&
                 htmlByte[0] == bomByte[0] && htmlByte[1] == bomByte[1]
                 && htmlByte[2] == bomByte[2] && htmlByte[3] == bomByte[3])
             {
                 codingName = "utf-32";
             }
             encoding = Encoding.BigEndianUnicode ;
             bomByte = encoding.GetPreamble();
             if (codingName == string.Empty && htmlByte.Length > bomByte.Length &&
                 htmlByte[0] == bomByte[0] && htmlByte[1] == bomByte[1])
             {
                 codingName = "utf-16";
             }

说明:上面的用到的GetByteContent方法,在下面第三种方法中有;

3,通过分析页面的meta提取编码

string encodingName = string.Empty;
WebRequest webRequest = WebRequest.Create(url);
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
byte[] htmlByte = GetByteContent(stream);
stream.Close();
string temp = Encoding.GetEncoding("utf-8").GetString(htmlByte);
string reg_charset = "(<meta[^>]*charset=(?<charset>[^>'\"]*)[\\s\\S]*?>)|(xml[^>]+encoding=(\"|')*(?<charset>[^>'\"]*)[\\s\\S]*?>)";
Regex r = new Regex(reg_charset, RegexOptions.IgnoreCase);
Match m = r.Match(temp);
encodingName = (m.Captures.Count != 0) ? m.Result("${charset}") : "";
string html = Encoding.GetEncoding(encodingName).GetString(htmlByte) ;
//GetByteContent函数
private byte[] GetByteContent(Stream stream)
{
        ArrayList arBuffer = new ArrayList();
        byte[] buffer = new byte[1024];
        int offset = 1024;
        int count = stream.Read(buffer, 0, offset);
        while (count > 0)
        {
            for (int i = 0; i < count; i++)
            {
                arBuffer.Add(buffer[i]);
            }
            count = stream.Read(buffer, 0, offset);
        }
        return (byte[])arBuffer.ToArray(System.Type.GetType("System.Byte"));
}

整理电脑上资料的时候翻出来的,不知道是什么年代的产物了。

Wednesday, March 20, 2013 | .NET技术

文章评论

No comments posted yet.

发表评论

Please add 4 and 4 and type the answer here:

关于博主

  一枚成分复杂的网络IT分子,属于互联网行业分类中的杂牌军。