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

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

 

抽个时间,写篇小文章,最近有个朋友,用vb开发一个工具,遇到WebClient获取的内容出现乱码,可惜对vb不是很熟悉,看了几分钟vb的语法,给他写了个编码转换的拿去用了。

毕竟咱是程序员,必须要有万全之策才可以啊,然后自己思考了一下,用C#码下以下代码,有需要的可以参考一下:

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

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

1 /// <summary> 2 /// 获取网页内容 3 /// </summary> 4 /// <param>目标url</param> 5 /// <returns>页面内容</returns> 6 public static string GetWebText(string url) 7 { 8 string result = "编码转换失败..."; 9 using (WebClient client = new WebClient()) 10 { 11 Stream stream = client.OpenRead(url); 12 using (StreamReader reader = new StreamReader(stream, client.Encoding)) 13 { 14 string text = reader.ReadToEnd(); 15 MatchCollection matchs = Regex.Matches(text, "charset=(.+)"); 16 if (matchs.Count > 0) 17 { 18 byte[] data = client.Encoding.GetBytes(text); 19 string charset = matchs[0].Groups[1].ToString().Trim(' ', '/', '>', '\r', '"'); 20 byte[] conver = Encoding.Convert(client.Encoding, Encoding.GetEncoding(charset), data); 21 result = Encoding.GetEncoding(charset).GetString(data); 22 } 23 } 24 } 25 return result; 26 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzfpxf.html