首页 > 代码库 > 正则获取页面编码 判断当前页面的编码

正则获取页面编码 判断当前页面的编码

 

        //根据指定网址判断当前页面的编码        static public string GetWebpageCode(string url)        {            string charSet = "";            WebClient myWebClient = new WebClient();                myWebClient.Credentials = CredentialCache.DefaultCredentials;            //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)             byte[] myDataBuffer = myWebClient.DownloadData(url);            string strWebData =http://www.mamicode.com/ Encoding.Default.GetString(myDataBuffer);            //获取网页字符编码描述信息             Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);            string webCharSet = charSetMatch.Groups[2].Value;            if (charSet == null || charSet == "")                charSet = webCharSet;                        return charSet;        }