获得当前系统的Response编码

/// <summary>
        /// 获得当前系统的Response编码
        /// </summary>
        /// <returns></returns>
        public static Encoding GetEncoding()
        {
            Encoding encoding = Encoding.UTF8;

            object config = WebConfigurationManager.GetWebApplicationSection("system.web/globalization");
            Type type1 = config.GetType();

            if (type1 != null)
            {
                PropertyInfo pinfo = type1.GetProperty("responseEncoding", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                if (pinfo != null)
                {
                    encoding = (Encoding)pinfo.GetValue(config, null);
                }
                else
                {
                    FieldInfo finfo = type1.GetField("responseEncoding", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    if (finfo != null)
                    {
                        encoding = (Encoding)finfo.GetValue(config);
                    }
                }
            }

            return encoding;
        }

你可能感兴趣的:(获得当前系统的Response编码)