欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#通过 WURFL 获取设备信息

程序员文章站 2022-06-12 17:13:21
...

我发现网上WURFL的代码非常少,最近工作时找了好久都找不到,让我来填补一下.NET使用WURFL的空缺吧!

使用语言:C#

环境:.net Framework 4.6.1 (当前使用) MVC4 (貌似支持所有NetWeb环境,我就不多说了)

直接上代码,不多BB:
记得NuGet最新的WURFL,那些我就不写了,都是很简单的

using System;
using System.Configuration;
using System.Diagnostics;
using WURFL;
using WURFL.Aspnet.Extensions.Config;
using System.Web.Caching;
using System.Web;

namespace WURFLandUA
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String userAgent = Request.UserAgent;
            var cache = CacheHelper.GetCache("WURFL_XML");//先读取 
            //如果检测到缓存没有则加载:首次加载约3600毫秒:二次加载约0.14毫秒

            if (cache == null)
            {
                WurflSampleASPNETApp.WurflManager = WURFLManagerBuilder.Build(new ApplicationConfigurer());
                CacheHelper.SetCache("WURFL_XML", WurflSampleASPNETApp.WurflManager);
            }
            else
            {
                WurflSampleASPNETApp.WurflManager = cache as IWURFLManager;
            }
            //下面是判断
            IDevice wurflDevice = WurflSampleASPNETApp.WurflManager.GetDeviceForRequest(Request);
            bool
              _android = bool.Parse(wurflDevice.GetVirtualCapability("is_android")) || userAgent.Contains("Android"),
               _iphone = bool.Parse(wurflDevice.GetVirtualCapability("is_ios")) || userAgent.Contains("iPhone")
               ;
            if (_android)
            {
                Response.Redirect(ConfigurationManager.AppSettings["AndroidPage"], false);
            }
            else if (_iphone)
            {
                Response.Redirect(ConfigurationManager.AppSettings["IosPage"], false);
            }
            else
            {
                Response.Write(ConfigurationManager.AppSettings["prompt"]);
            }

        }
    }
    public static class WurflSampleASPNETApp
    {
        public static IWURFLManager WurflManager;
    }
}

因为每次都读取他的XML太慢,所以,我加了缓存处理,为了以防万一,还是加了用户代理判断(UA)
看不懂的可以联系我。私信、qq都行,回复也行,现在是CSDN的常客

之前忘记加上缓存帮助类了,给大家带来不便,现在加上

    public class CacheHelper
    {
        /// <summary>
        ///     获取数据缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        public static object GetCache(string cacheKey)
        {
            var objCache = HttpRuntime.Cache.Get(cacheKey);
            return objCache;
        }
        /// <summary>
        ///     设置数据缓存
        /// </summary>
        public static void SetCache(string cacheKey, object objObject)
        {
            var objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, objObject);
        }

        /// <summary>
        ///     设置数据缓存
        /// </summary>
        public static void SetCache(string cacheKey, object objObject, int timeout = 3600)
        {
            try
            {
                if (objObject == null) return;
                var objCache = HttpRuntime.Cache;
                //相对过期
                //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
                //绝对过期时间
                objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, CacheItemPriority.High, null);
            }
            catch (Exception)
            {
                //throw;
            }
        }
        /// <summary>
        ///     移除指定数据缓存
        /// </summary>
        public static void RemoveAllCache(string cacheKey)
        {
            var cache = HttpRuntime.Cache;
            cache.Remove(cacheKey);
        }
        /// <summary>
        ///     移除全部缓存
        /// </summary>
        public static void RemoveAllCache()
        {
            var cache = HttpRuntime.Cache;
            var cacheEnum = cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                cache.Remove(cacheEnum.Key.ToString());
            }
        }
    }