判断是否是手机访问及手机操作系统
我们现在做的项目,移动端和PC端网站访问的是同一套接口,那么如何在接口中区分是手机访问还是PC短访问呢?
核心思想:根据请求头(request header)来判断,如何请求没有header或伪造user agent则无法判断.
先看一些user agent的实例:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) (Engine, like URL) Mobile/12B440 MicroMessenger/6.0.1 NetType/3G+
Mozilla/5.0 (Linux; U; Android 4.1.1; en-cn; HTC T528d) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/6.0.2.58_r984381.520 NetType/WIFI
Mozilla/5.0 (Linux; U; Android 4.2.2; zh-CN; 2013023 Build/HM2013023) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/9.9.5.489 U3/0.8.0 Mobile Safari/533.1
Opera/9.80 (Android 2.3.7; Linux; Opera Mobi/46154) Presto/2.11.355 Version/12.10
Mozilla/5.0 (Linux; U; Android 4.2.1; zh-cn; HUAWEI G700-U00 Build/HuaweiG700-U00) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 V1_AND_SQ_5.3.1_196_YYB_D QQ/5.3.1.2335 NetType/WIFI
Mozilla/5.0 (Linux; U; Android 4.0.4; zh-cn; HS-EG906 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/5.3.1.67_r745169.462
Mozilla/5.0 (Linux; U; Android 4.4.2; zh-cn; GT-I9500 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.0 QQ-Manager Mobile Safari/537.36
以下是我封装的一套方法
ClientOsInfo 是用于保存访问设备信息的,结构如下(省略getter,setter):
- public class ClientOsInfo {
- /***
- * 比如 Android_3.0
- */
- private String osTypeVersion;
- /***
- * Pad或Phone
- */
- private String deviceType;
- /***
- * os type
- */
- private String osType;
- /***
- * 只是版本号,例如"4.1.1"
- */
- private String version;
- private String userAgent;
- /***
- * 是否是移动设备
- * @return
- */
- public boolean isMobile(){
- return (!ValueWidget.isNullOrEmpty(this.deviceType));
- }
- }
核心方法:
- /***
- * 当移动端(手机或Pad)访问网页时获取移动端操作系统信息
- * @param request
- * @return
- */
- public static ClientOsInfo getMobileOsInfo(HttpServletRequest request){
- String userAgent=request.getHeader("user-agent");
- if(ValueWidget.isNullOrEmpty(userAgent)){
- userAgent=request.getHeader("User-Agent");
- }
- ClientOsInfo info= HeaderUtil.getMobilOS(userAgent);
- info.setUserAgent(userAgent);
- return info;
- }
核心工具类:用于解析user agent
- package com.common.util;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import com.common.bean.ClientOsInfo;
- import com.string.widget.util.ValueWidget;
- /***
- *
- * @author huangwei
- * @since 2013-08-15
- */
- public class HeaderUtil {
- public static final String OSTYPE_ANDROID="Android";
- public static final String OSTYPE_IOS="Ios";
- public static final String OSTYPE_WP="WINDOWS PHONE";
- public static final String OSTYPE_BLACKBERRY="BLACKBERRY";
- /***
- * pad
- */
- public static final String DEVICE_TYPE_PAD="Pad";
- /***
- * 手机
- */
- public static final String DEVICE_TYPE_PHONE="Phone";
- /***
- * 校验渠道终端版本号是否合法,eg:0.0.0.3
- *
- * @param clientVersion
- * @return true-->合法 ;false-->非法
- */
- public static boolean verifyClientVersion(String clientVersion) {
- boolean result = Pattern.matches("[\\d\\.]+", clientVersion);
- if (result) {
- result = Pattern.matches("^\\d\\.\\d\\.\\d\\.\\d$", clientVersion);
- return result;
- } else {
- return false;
- }
- }
- /**
- * 根据useragent和手机厂商查手机型号
- *
- * @param UA
- * @param vendor
- * @return
- */
- public static String getMobModel(String UA, String operatingSystem) {
- if (UA == null) {
- return null;
- }
- // 存放正则表达式
- String rex = "";
- // 苹果产品
- if (operatingSystem.indexOf("IOS") != -1) {
- if (UA.indexOf("IPAD") != -1) {// 判断是否为ipad
- return "IPAD";
- }
- if (UA.indexOf("IPOD") != -1) {// 判断是否为ipod
- return "IPOD";
- }
- if (UA.indexOf("IPONE") != -1) {// 判断是否为ipone
- return "IPONE";
- }
- return "IOS DEVICE";
- }
- // 安卓系统产品
- if (operatingSystem.indexOf("ANDROID") != -1) {
- String re = "BUILD";
- rex = ".*" + ";" + "(.*)" + re;
- Pattern p = Pattern.compile(rex, Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(UA);
- boolean rs = m.find();
- if (rs) {
- System.out.println("Mobil Model is" + m.group(1));
- return m.group(1);
- }
- }
- return null;
- }
- /**
- * 判断手机的操作系统 IOS/android/windows phone/BlackBerry
- *
- * @param UA
- * @return
- */
- public static ClientOsInfo getMobilOS(String UA) {
- UA=UA.toUpperCase();
- if (UA == null) {
- return null;
- }
- ClientOsInfo osInfo=new ClientOsInfo();
- // 存放正则表达式
- String rex = "";
- // IOS 判断字符串
- String iosString = " LIKE MAC OS X";
- if (UA.indexOf(iosString) != -1) {
- if(isMatch(UA, "\\([\\s]*iPhone[\\s]*;", Pattern.CASE_INSENSITIVE)){
- osInfo.setDeviceType(DEVICE_TYPE_PHONE);
- }else if(isMatch(UA, "\\([\\s]*iPad[\\s]*;", Pattern.CASE_INSENSITIVE)){
- osInfo.setDeviceType(DEVICE_TYPE_PAD);
- }
- rex = ".*" + "[\\s]+(\\d[_\\d]*)" + iosString;
- Pattern p = Pattern.compile(rex, Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(UA);
- boolean rs = m.find();
- if (rs) {
- String osVersion= m.group(1).replace("_", ".");
- osInfo.setVersion(osVersion);
- // System.out.println("Mobil OS is" + " IOS" +osVersion);
- osInfo.setOsTypeVersion(OSTYPE_IOS+"_" + osVersion);
- }else{
- System.out.println("IOS");
- osInfo.setOsTypeVersion(OSTYPE_IOS);
- }
- osInfo.setOsType(OSTYPE_IOS);
- return osInfo;
- }
- // Android 判断
- String androidString = "ANDROID";
- if (UA.indexOf(androidString) != -1) {
- if(isMatch(UA, "\\bMobi", Pattern.CASE_INSENSITIVE)){
- osInfo.setDeviceType(DEVICE_TYPE_PHONE);
- }else {
- osInfo.setDeviceType(DEVICE_TYPE_PAD);
- }
- rex = ".*" + androidString + "[\\s]*(\\d*[\\._\\d]*)";
- Pattern p = Pattern.compile(rex, Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(UA);
- boolean rs = m.find();
- if (rs) {
- String version=m.group(1).replace("_", ".");
- osInfo.setVersion(version);
- System.out.println("Mobil OS is " + OSTYPE_ANDROID + version);
- osInfo.setOsTypeVersion(OSTYPE_ANDROID+"_" + version);
- }else{
- System.out.println("Android");
- osInfo.setOsTypeVersion(OSTYPE_ANDROID);
- }
- osInfo.setOsType(OSTYPE_ANDROID);
- return osInfo;
- }
- // windows phone 判断
- String wpString = "WINDOWS PHONE";
- if (UA.indexOf(wpString) != -1) {
- rex = ".*" + wpString + "[\\s]*[OS\\s]*([\\d][\\.\\d]*)";
- Pattern p = Pattern.compile(rex, Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(UA);
- boolean rs = m.find();
- if (rs) {
- System.out.println("Mobil OS is " + OSTYPE_WP + m.group(1));
- String version=m.group(1);
- osInfo.setVersion(version);
- osInfo.setOsTypeVersion(OSTYPE_WP+"_" + version);
- }else{
- System.out.println("WINDOWS PHONE");
- osInfo.setOsTypeVersion(OSTYPE_WP);
- }
- osInfo.setOsType(OSTYPE_WP);
- return osInfo;
- }
- // BlackBerry 黑莓系统判断
- String bbString = "BLACKBERRY";
- if (UA.indexOf(bbString) != -1) {
- rex = ".*" + bbString + "[\\s]*([\\d]*)";
- Pattern p = Pattern.compile(rex, Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(UA);
- boolean rs = m.find();
- if (rs) {
- System.out.println("Mobil OS is" + " BLACKBERRY " + m.group(1));
- String version=m.group(1);
- osInfo.setVersion(version);
- osInfo.setOsTypeVersion(OSTYPE_BLACKBERRY+"_" + version);
- }else{
- System.out.println("BLACKBERRY");
- osInfo.setOsTypeVersion(OSTYPE_BLACKBERRY);
- }
- osInfo.setOsType(OSTYPE_BLACKBERRY);
- return osInfo;
- }
- if(UA.contains("LINUX")){//android
- if(isMatch(UA, "\\bMobi", Pattern.CASE_INSENSITIVE)){
- osInfo.setDeviceType(DEVICE_TYPE_PHONE);
- }else {
- osInfo.setDeviceType(DEVICE_TYPE_PAD);
- }
- Pattern p = Pattern.compile("U;\\s*(Adr[\\s]*)?(\\d[\\.\\d]*\\d)[\\s]*;",Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(UA);
- boolean result = m.find();
- String find_result = null;
- if (result)
- {
- find_result = m.group(2);
- }
- if(ValueWidget.isNullOrEmpty(find_result)){
- osInfo.setOsTypeVersion(OSTYPE_ANDROID);
- return osInfo;
- }else{
- osInfo.setVersion(find_result);
- osInfo.setOsTypeVersion(OSTYPE_ANDROID+"_"+find_result);
- return osInfo;
- }
- }
- //UCWEB/2.0 (iOS; U; iPh OS 4_3_2; zh-CN; iPh4)
- if(UA.matches(".*((IOS)|(iPAD)).*(IPH).*")){
- if(isMatch(UA, "[\\s]*iPh[\\s]*", Pattern.CASE_INSENSITIVE)){
- osInfo.setDeviceType(DEVICE_TYPE_PHONE);
- }else {
- osInfo.setDeviceType(DEVICE_TYPE_PAD);
- }
- Pattern p = Pattern.compile("U;\\s*(IPH[\\s]*)?(OS[\\s]*)?(\\d[\\._\\d]*\\d)[\\s]*;",Pattern.CASE_INSENSITIVE);
- Matcher m = p.matcher(UA);
- boolean result = m.find();
- String find_result = null;
- if (result)
- {
- find_result = m.group(3);
- }
- if(ValueWidget.isNullOrEmpty(find_result)){
- osInfo.setOsTypeVersion(OSTYPE_IOS);
- osInfo.setOsType(OSTYPE_IOS);
- return osInfo;
- }else{
- String version=find_result.replace("_", ".");
- osInfo.setVersion(version);
- osInfo.setOsTypeVersion(OSTYPE_IOS+"_"+version);
- osInfo.setOsType(OSTYPE_IOS);
- return osInfo;
- }
- }
- return osInfo;
- }
- public static boolean isMatch(String source,String regx,int flags){
- Pattern p = Pattern.compile(regx,flags);
- Matcher m = p.matcher(source);
- boolean result = m.find();
- return result;
- }
- }
应用场景:
通过user agent判断是否是手机
- /***
- * 判断是否是手机(移动端)
- * @param userAgent
- * @return
- */
- public static boolean isMobile(String userAgent)
- {
- userAgent=userAgent.toLowerCase();
- String []mobile_agents = {"240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte"};
- boolean is_mobile = false;
- for (String device : mobile_agents) {
- if(userAgent.contains(device)){
- is_mobile=true;
- break;
- }
- }
- return is_mobile;
- }
测试代码:
- @Test
- public void test_userAgent()
- {
- String userAgent="Mozilla/5.0 (Linux; U; Android 4.1.1; en-cn; HTC T528d) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/6.0.2.58_r984381.520 NetType/WIFI";
- System.out.println(HeaderUtil.isMobile(userAgent));
- userAgent="Opera/9.80 (Android 2.3.7; Linux; Opera Mobi/46154) Presto/2.11.355 Version/12.10";
- System.out.println(HeaderUtil.isMobile(userAgent));
- }
判断是否是手机访问及手机操作系统相关推荐
- 华为手机访问文件服务器,手机访问云服务器文件
手机访问云服务器文件 内容精选 换一换 华为云帮助中心,为用户提供产品简介.价格说明.购买指南.用户指南.API参考.最佳实践.常见问题.视频帮助等技术文档,帮助您快速上手使用华为云服务. 云服务器创 ...
- php怎么判断是否手机网站,php,js判断网站是不是手机访问
Warning: strncmp() has been disabled for security reasons in /www/web/default/blog/public_html/wp-in ...
- 自动切换电脑或手机版(php aspx),ASP程序自动判断是电脑或手机访问网站。
很早就知道百度可以用电脑和手机访问同一个地址,但是网站会自动根据使用的上网终端选择跳转到不同的页面. 曾经试着用JS去判断,但是JS只是浏览器脚本,似乎没有这么强大的功能去获取电脑和手机各种不同浏览器 ...
- php判断电脑浏览器模拟手机访问网页,在PC上测试移动端网站和模拟手机浏览器的5大方法...
查了很多资料,尝试了大部分方法,下面将这一天的努力总结下分享给大家,也让大家免去看那么多文章,以下介绍的方法,都是本人亲自测试成功的方法,测试环境winxp. 一.Chrome*浏览器 chrome模 ...
- PHP CodeBase: 判断用户是否手机访问
转载地址:http://www.nowamagic.net/librarys/veda/cate/PHP 随着移动设备的普及,网站也会迎来越来越多移动设备的访问.用适应PC的页面,很多时候对手机用户不 ...
- 判断用户用手机访问还是用电脑访问网页
C# 判断用户用手机访问还是用电脑访问网页 string osPat = "mozilla|m3gate|winwap|openwave|Windows NT|Windows 3.1|95| ...
- 通过PHP自带的$_SERVER判断 手机访问网站自动跳转到手机版
需要有PC版网站和移动版网站,当手机访问域名的时候,通过PHP自带的$_SERVER判断浏览设备并跳转到移动版页面.如果是PC电脑打开网址,则不跳转直接访问pc版. 1 <?php 2 //ph ...
- java 判断手机访问_下面java代码判断是手机访问还是PC访问什么地方出错了,手机跳转不到制定页面,等待解答...
最近在做一个项目,要在判断用户是否是通过手机访问网站的,如果是手机访问网站跳到一个登录页面,如果是用户用电脑访问网站跳到另外一个登录页面.我写了以下代码,但是用手机访问网站的时候,跳转不到制定页面,请 ...
- django项目内部的views层判断手机访问还是电脑访问
在做网站时,一般都会通过打包html来制作两个界面,一个提供给电脑端,一个提供给手机端,避免因为兼容性差的问题,界面会变得很丑.当然,如果能保证纯bootstrap开发html界面,则不需要考虑这个问 ...
最新文章
- TF-IDF与余弦相似性的应用(一):自动提取关键词
- blockly和Java交互_blockly 基础学习(一)
- html2image乱码问题,HtmlImageGenerator字体乱码问题解决、html2image放linux上乱码问题解决...
- c++ stl 容器 迭代器 stl用法示例
- 信息学奥赛C++语言:哥德巴赫猜想
- XAMPP修改80和443端口及创建虚拟目录
- Codeforces Round #263 (Div.1) B. Appleman and Tree
- Redhat(Linux)上的JBoss管理配置
- 《数据库系统概念》14-静态散列
- 人有多大懒,才有多大闲 (评论『卓有成效的程序员』)
- Eclipse使用Git上传新项目到GitHub
- 金蝶KIS专业版V14.1生产任务单|销售单等单据图片打印
- 【Qt】Qt5.14.2配置yaml-cpp
- TFIDF的原理及实现
- 点云采样FPS原理及实现
- Python实现的简易HTTP代理服务器
- java使用aspose打印pdf、word文件
- html加水平线紧挨文字,html怎么设置水平线与文字的距离
- 十大最佳外国Android游戏下载平台
- SAP FICO 第一节 后台配置