JAVA-determine whether the request comes from the PC or mobile phone, java-pc

Source: Internet
Author: User

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 booleanisMobileDevice(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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.