In some cases, we need to determine whether the HTTP request is from the phone or the computer side, the key is to obtain user-agent information, screening and judgment can be.
The core classes are as follows:
| 1234567891011121314151617 |
publicstaticbooleanisMobileDevice(String requestHeader){ /** * android : 所有android设备 * mac os : iphone ipad * windows phone:Nokia等windows系统的手机 */ String[] deviceArray = newString[]{"android","mac os","windows phone"}; if(requestHeader == null) returnfalse; requestHeader = requestHeader.toLowerCase(); for(inti=0;i<deviceArray.length;i++){ if(requestHeader.indexOf(deviceArray[i])>0){ return true; } } returnfalse;} |
Get the HTTP header information in the controller as follows:
| 123456 |
String requestHeader = request.getHeader("user-agent"); if(JudgeRequestDeviceUtil.isMobileDevice(requestHeader)){ logger.debug("使用手机浏览器"); }else{ logger.debug("使用web浏览器"); } |
From:
Java-judging request from PC or mobile