JAVA-determine whether the request comes from the PC or mobile phone, java-pc
In some cases, we need to determine whether the Http request comes from a mobile phone or a computer. The key is to obtain the User-Agent information and perform filtering and determination.
The core classes are as follows:
1234567891011121314151617 |
public static boolean isMobileDevice(String requestHeader){ /** * Android: All android devices * mac os : iphone ipad * Windows phone: mobile phones for windows systems such as Nokia */ String[] deviceArray = new String[]{ "android" , "mac os" , "windows phone" }; if (requestHeader == null ) return false ; requestHeader = requestHeader.toLowerCase(); for ( int i= 0 ;i<deviceArray.length;i++){ if (requestHeader.indexOf(deviceArray[i])> 0 ){ return true ; } } return false ; } |
Obtain the http header information in the controller as follows:
123456 |
String requestHeader = request.getHeader( "user-agent" ); if (JudgeRequestDeviceUtil.isMobileDevice(requestHeader)){ logger.debug( "Using a mobile browser" ); } else { logger.debug( "Using web browsers" ); } |
From: