zoukankan      html  css  js  c++  java
  • 获取页面编码的方法

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

    WebRequest webRequest = WebRequest.Create(url);
    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
    Regex reg_charset = new Regex(@"charsets*=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;
    }

    引用地址 http://blog.useasp.net/default.aspx

    2. 通过网页分析

     1 //获取页面
     2                             string strResult = Encoding.Default.GetString(e.Result);
     3  
     4                             const string regCharset = "(<meta[^>]*charset=(?<charset>[^>'"]*)[\s\S]*?>)|(xml[^>]+encoding=("|')*(?<charset>[^>'"]*)[\s\S]*?>)";
     5 
     6                             var r = new Regex(regCharset, RegexOptions.IgnoreCase);
     7                             var m1 = r.Match(strResult);
     8                             string encodingName = (m1.Captures.Count != 0) ? m1.Groups["charset"].Value : "";
     9 
    10                             if (string.IsNullOrEmpty(encodingName))
    11                             {
    12                                 //如果未获取  这手动替换判断
    13                                 string str = m1.Groups[1].Value;
    14                                 const string pattern = "<meta charset="|">|" />";
    15                                 encodingName = Regex.Replace(str, pattern, "");
    16 
    17                             }
  • 相关阅读:
    PostgreSQL 模式(SCHEMA)
    PostgreSQL学习---模式schema
    psql 工具详细使用介绍
    CentOS 下 VNC Server 的配置与使用
    如何处理/boot/efi/EFI/cento from install of fwupdate-efi
    uGUI练习(二) Animate UI
    uGUI练习(一) Anchor
    uGUI练习 开篇
    Fix "Missing Scripts"
    用uGUI开发自定义Toggle Slider控件
  • 原文地址:https://www.cnblogs.com/prolion/p/3469170.html
Copyright © 2011-2022 走看看