我们现在做的项目,移动端和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)); }
源代码见附件,工具类:com.common.util.HeaderUtil
方法 getMobileOsInfo在com.common.util.WebServletUtil
判断微信
public static boolean isMicromessengerBrowser(HttpServletRequest request) { String userAgent = request.getHeader("user-agent").toLowerCase(); return userAgent.contains("micromessenger"); }
作者:黄威
主页:黄威的技术博客
相关推荐
"php判断手机访问"这个主题主要涉及到如何利用PHP来检测用户代理(User-Agent)字符串,从而识别访问者使用的设备类型。 User-Agent是一个HTTP头部字段,由浏览器发送给服务器,包含了浏览器的类型、版本以及操作...
### JS判断是手机访问还是电脑访问 在现代Web开发中,根据用户访问设备的不同来提供定制化的用户体验变得越来越重要。本文将详细介绍如何使用JavaScript(简称JS)来判断用户是通过手机还是电脑访问网站的方法。 #...
在Web开发中,判断访问者是否使用手机设备是一项常见的需求,这有助于提供适应不同设备的用户体验,例如响应式设计或特定于移动设备的功能。在这个场景中,我们可以利用PHP编程语言来实现这一功能。"detectmobile...
除了这些基本的判断方法,Mobile-Detect库还支持检测特定的设备型号,例如iPhone、iPad、Android手机或平板等,以及检测特定的操作系统,如iOS、Android、Windows Phone等。这使得开发者能够针对不同设备提供定制化...
以上就是利用JavaScript判断用户是否使用手机访问网站的基本原理和实现方法。通过这些技术,开发者可以创建更加智能和适应性的网页,提高不同设备用户的浏览体验。当然,随着设备种类的增加,可能需要不断更新检测...
如果字符串中包含这些关键词,则判断为手机访问。 #### 检查HTTP_ACCEPT头部 HTTP_ACCEPT头部包含了客户端可接受的MIME类型,这通常用于确定客户端可以接收的内容类型。例如,某些移动设备可能更偏好WML(Wireless...
通过解析这个字符串,我们可以获取到设备类型、操作系统、浏览器版本等信息。 以下是一个简单的手机浏览器检测的PHP代码示例: ```php function isMobileDevice() { if (isset($_SERVER['HTTP_X_WAP_PROFILE']) |...
在开发Web应用时,识别用户访问的设备类型和操作系统至关重要,这可以帮助我们为不同平台提供最佳的用户体验。本文将深入探讨如何使用原生JavaScript来判断手机终端,特别是针对安卓(Android)系统和iOS系统。 ...
在网页开发中,判断用户访问的是手机端还是桌面端是一个常见的需求,这有助于提供不同的页面布局或功能,以优化用户体验。这段"检测是否是手机端的JS代码"就是解决这个问题的关键。下面,我们将深入探讨这个知识点,...
"JS判断手机端与电脑访问"这个话题就是关于如何使用JavaScript来检测用户是通过移动设备(如智能手机或平板)还是桌面电脑(如笔记本或台式机)访问网站的。这通常涉及到设备的屏幕尺寸、触屏支持、浏览器特性等因素...
JavaScript是前端开发中常用的编程语言,它可以用来动态地检查用户的浏览器信息,如User-Agent字符串,以判断用户是通过PC还是手机访问网站。 实现这种功能的具体步骤可能如下: 1. **获取User-Agent**:首先,...
`User-Agent`包含了浏览器和操作系统的信息,可以根据其特征判断是否来自手机。例如,许多手机浏览器会在`User-Agent`字符串中包含"Mobile"、"Android"、"iPhone"等关键词。 - **屏幕尺寸检测**:另一种方式是检测...
为了实现这一目标,开发者需要能够识别用户的访问设备,比如是否为手机,以及浏览器和操作系统的类型和版本。这里,我们关注的是一个名为"Browser.class.php"的PHP类库,它提供了这样的功能。配合"demo.php"这个示例...
标题所提及的"手机APP下载页面判断是否微信",主要涉及到的技术点是检测用户访问页面的环境,特别是当用户在微信内置浏览器中打开APK下载链接时,如何确保用户能够顺利下载。微信为了安全和用户体验,限制了直接在...
User Agent String(简称UA字符串)就是一个关键工具,它包含了客户端浏览器的详细信息,如浏览器名称、版本、操作系统等。本资源提供了基于Java和PHP的解决方案,帮助开发者解析和识别UA字符串,从而判断客户端平台...
这可以通过安装UserAgentSwitcher插件来实现,从而允许用户在PC上访问手机版或触屏版网页。 3. 浏览器设置更改:在Chrome浏览器中更改用户代理的步骤包括访问浏览器的设置,进入扩展程序页面,搜索并安装UserAgent...
本文将深入探讨如何使用JavaScript来判断用户访问的是手机、平板还是桌面电脑,以便于根据设备特性提供相应的网页体验。 首先,我们要了解不同设备的浏览器发送的User-Agent字符串,它包含了关于浏览器的信息,包括...
总而言之,服务器端使用PHP脚本来判断用户是否通过手机访问网站,并引导其跳转至相应的移动版页面,是一种提高用户体验的有效方式。开发者需要结合具体应用场景和网站的需求,灵活运用各种检测方法,并定期更新检测...
你可以分别用电脑和手机访问这个页面,以验证判断功能是否准确。 总结: 通过分析HTTP头信息,我们可以利用PHP有效地识别出访问网站的设备类型。这在构建响应式网站或需要针对不同设备定制体验的应用中非常关键。`...