c# , .net 后端判断是否是移动端,是否是安卓设备,是否是微信,是否是支付宝,是否是QQ,是否是安卓系统,是否是苹果系统等

c# , .net 后端判断是否是移动端,是否是安卓设备,是否是微信,是否是支付宝,是否是QQ,是否是安卓系统,是否是苹果系统等,第1张

c# , .net 判断是否是移动端,是否是微信,支付宝,QQ,是什么系统等

仅支持web项目

第一步:拿到请求上下文

我是项目是MVC项目

方法一:

var UserAgentText= Request.UserAgent;

方法二:

var request = Request.Headers;
string UserAgentText= request["User-Agent"];
第二步:获取设备信息
 UserAgentDeviceInfoModel userAgentDeviceInfoModel = UserAgentHelp.GetDeviceInfo(UserAgentText); 

示例:
    public class UserAgentExecute
    {
        public static void Execute()
        {
            List listStr = new List();

            /【电脑-谷歌浏览器信息】User-Agent/
            listStr.Add("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36");

            /【电脑-360浏览器信息】User-Agent/
            listStr.Add("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");

            /【手机微信-浏览器信息】User-Agent/
            listStr.Add("Mozilla/5.0 (Linux; Android 10; M2002J9E Build/QKQ1.191222.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/86.0.4240.99 XWEB/3209 MMWEBSDK/20220204 Mobile Safari/537.36 MMWEBID/3181 MicroMessenger/8.0.20.2100(0x2800149A) Process/toolsmp WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64");

            /【苹果手机微信-浏览器信息】User-Agent/
            listStr.Add("Mozilla/5.0 (iPhone; CPU iPhone OS 15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.18(0x1800123f) NetType/WIFI Language/zh_CN");

            /【手机QQ-浏览器信息】User-Agent/
            listStr.Add("Mozilla/5.0 (Linux; Android 10; M2002J9E Build/QKQ1.191222.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/89.0.4389.72 MQQBrowser/6.2 TBS/045913 Mobile Safari/537.36 V1_AND_SQ_8.8.83_2654_YYB_D A_8088300 PA QQ/8.8.83.7540 NetType/WIFI WebP/0.3.0 Pixel/1080 StatusBarHeight/70 SimpleUISwitch/0 QQTheme/1000 InMagicWin/0 StudyMode/0 CurrentMode/0 CurrentFontScale/1.0 GlobalDensityScale/0.9818182 AppId/537114460");

            /【苹果手机-QQ-浏览器信息】User-Agent/
            listStr.Add("Mozilla/5.0 (iPhone; CPU iPhone OS 15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/19E241 QQ/8.8.85.636 V1_IPH_SQ_8.8.85_1_APP_A Pixel/1170 MiniAppEnable SimpleUISwitch/1 StudyMode/0 CurrentMode/1 CurrentFontScale/1.000000 QQTheme/2921 AppId/537115277 Core/WKWebView Device/Apple(iPhone 13) NetType/WIFI QBWebViewType/1 WKType/1");

            /【手机支付宝-浏览器信息】User-Agent/
            listStr.Add("Mozilla/5.0 (Linux; U; Android 10; zh-CN; M2002J9E Build/QKQ1.191222.002) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 UWS/3.22.2.43 Mobile Safari/537.36 UCBS/3.22.2.43_220223200704 NebulaSDK/1.8.100112 Nebula AlipayDefined(nt:WIFI,ws:393|0|2.75) AliApp(AP/10.2.58.8000) AlipayClient/10.2.58.8000 Language/zh-Hans useStatusBar/true isConcaveScreen/true Region/CNAriver/1.0.0");

            /【苹果手机-支付宝-浏览器信息】User-Agent/
            listStr.Add("Mozilla/5.0 (iPhone; CPU iPhone OS 15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/19E241 Ariver/1.1.0 AliApp(AP/10.2.59.2500) Nebula WK RVKType(1) AlipayDefined(nt:WIFI,ws:390|780|3.0) AlipayClient/10.2.59.2500 Alipay Language/zh-Hans Region/CN NebulaX/1.0.0");


            UserAgentDeviceInfoModel userAgentDeviceInfoModel = null;
            foreach (string UserAgentText in listStr)
            {
                userAgentDeviceInfoModel = UserAgentHelp.GetDeviceInfo(UserAgentText); 
                Console.WriteLine(userAgentDeviceInfoModel.Remark);
            }
        }

       
    }
结果:
系统:Windows;应用名称:unknown;是否移动端:False;
系统:Windows;应用名称:unknown;是否移动端:False;
系统:Android;应用名称:WeChat;是否移动端:True;
系统:iPhone;应用名称:WeChat;是否移动端:True;
系统:Android;应用名称:QQ;是否移动端:True;
系统:iPhone;应用名称:QQ;是否移动端:True;
系统:Android;应用名称:Alipay;是否移动端:True;
系统:iPhone;应用名称:Alipay;是否移动端:True;

核心帮助类UserAgentHelp:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test1.Request.UserAgent
{
   public class UserAgentHelp
    {
        /// 
        /// 获取设备信息
        /// 
        /// 
        /// 
        public static UserAgentDeviceInfoModel GetDeviceInfo(string UserAgentText)
        {
            UserAgentDeviceInfoModel userAgentDeviceInfoModel = new UserAgentDeviceInfoModel()
            {
                System = ChackSystemInfo(UserAgentText),
                App = ChackAppInfo(UserAgentText),
                IsMobile = UserAgentText.Contains("Mobile")
            };
            userAgentDeviceInfoModel.Remark = $"系统:{userAgentDeviceInfoModel.System};应用名称:{userAgentDeviceInfoModel.App};是否移动端:{userAgentDeviceInfoModel.IsMobile};";

            return userAgentDeviceInfoModel;
        }
        /// 
        /// 检查系统信息
        /// 
        /// 
        /// 
        private static DeviceSystemEnum ChackSystemInfo(string str)
        {
            if (str.Contains("Android"))
            {
                return DeviceSystemEnum.Android;
            }
            else if (str.Contains("iPhone"))
            {
                return DeviceSystemEnum.iPhone;
            }
            else if (str.Contains("Windows"))
            {
                return DeviceSystemEnum.Windows;
            }
            else
            {
                return DeviceSystemEnum.unknown;//未知
            }
        }
        /// 
        /// 检查应用信息
        /// 
        /// 
        /// 
        private static DeviceAppEnum ChackAppInfo(string str)
        {
            if (str.Contains("MicroMessenger"))
            {
                return DeviceAppEnum.WeChat;
            }
            else if (str.Contains("AlipayClient"))
            {
                return DeviceAppEnum.Alipay;
            }
            else if (str.Contains("QQ"))
            {
                return DeviceAppEnum.QQ;
            }
            else
            {
                return DeviceAppEnum.unknown;//未知
            }
        }
    }
    /// 
    /// 用户请求的设备信息
    /// 
    public class UserAgentDeviceInfoModel
    {
        /// 
        /// 系统信息
        /// 
        public DeviceSystemEnum System { set; get; }
        /// 
        /// 应用信息
        /// 
        public DeviceAppEnum App { set; get; }
        /// 
        /// 是否移动端
        /// 
        public bool IsMobile { set; get; }
        /// 
        /// 备注信息
        /// 
        public string Remark { set; get; }
    }

    /// 
    /// 系统信息枚举
    /// 
    public enum DeviceSystemEnum
    {
        Android,
        iPhone,
        Windows,
        /// 
        /// 未知
        /// 
        unknown,
    }

    /// 
    /// 应用名称信息枚举
    /// 
    public enum DeviceAppEnum
    {
        /// 
        /// 微信-(判断字段MicroMessenger)
        /// 
        WeChat,
        /// 
        /// 支付宝-(判断字段AlipayClient)
        /// 
        Alipay,
        /// 
        /// qq-(判断字段QQ)
        /// 
        QQ,
        /// 
        /// 未知
        /// 
        unknown,
    }
}


扩展阅读

HTTP Header 详解,headers的值都是什么意思_cplvfx的博客-CSDN博客HTTP Header 详解https://blog.csdn.net/cplvfx/article/details/124064184

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/989927.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存