判断请求来源是否PC

判断请求是否来自Windows系统的PC端,可以通过解析HTTP请求头中的User-Agent字段来实现,User-Agent是浏览器或客户端在请求中自动携带的标识字符串,包含了设备类型、操作系统、浏览器版本等信息。通过检测其中是否包含​​Windows系统的关键词​​,即可判断请求是否来自Windows PC。

["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod","OpenHarmony"
]

public static boolean isMobileDevice(String userAgent) {
    String[] mobileKeywords = {"Android", "iPhone", "iPad", "Mobile", "Windows Phone"};
    if (userAgent == null) return false;
    String lowerUserAgent = userAgent.toLowerCase();
    for (String keyword : mobileKeywords) {
        if (lowerUserAgent.contains(keyword.toLowerCase())) {
            return true;
        }
    }
    return false;
}

 

posted @ 2025-04-08 15:55  凉了记忆  阅读(61)  评论(0)    收藏  举报