Java obtains the browser type, operating system type, and mobile phone type through the browser Request Header (User-Agent ).

Source: Internet
Author: User

Java obtains the browser type, operating system type, and mobile phone type through the browser Request Header (User-Agent ).
The Chinese name of the User Agent is the User Agent (UA), which is a special string header, the server can identify the operating system and version, CPU type, browser and version, browser rendering engine, browser language, and browser plug-in used by the customer.

Some websites often send different pages to different operating systems and browsers by judging the UA. Therefore, some pages may not be displayed normally in a browser, however, you can bypass detection by disguising UA.

1. Obtain the User-Agent in the browser Request Header


String ua = request.getHeader("User-Agent")

2. Obtain the browser type and operating system type: (Note that the UserAgent class is in UserAgentUtils. jar and downloaded by yourself)



    UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));             Browser browser = userAgent.getBrowser();             OperatingSystem os = userAgent.getOperatingSystem();  

3. Obtain the mobile phone type:



Solution 1: Regular Expressions

By observing the law, the following expressions are obtained:

;\s?([^;]+?)\s?(Build)?/  

Java code:


Pattern pattern = Pattern. compile (";\\ s? (\ S *? \ S? \ S *?) \ S? (Build )? /"); Matcher matcher = pattern. matcher (userAgent); String model = null; if (matcher. find () {model = matcher. group (1 ). trim (); log. debug ("parse the model through userAgent:" + model );}

The following are some useragents for testing and can be verified directly in EditPlus.



Mozilla/5.0 (Linux; U; Android 4.3; zh-cn; R8007 Build/JLS36C) AppleWebKit/534.30 (KHTML, like Gecko) version/4.0 Mobile Safari/534.30 Mozilla/5.0 (Linux; U; Android 4.3; zh-cn; R8007 Build/JLS36C) AppleWebKit/534.30 (KHTML, like Gecko) version/4.0 Mobile Safari/534.30 V1_AND_SQ_5.0.0_146_YYB_D QQ/5.0.0.2215Mozilla/5.0 (Linux; U; Android 4.3; zh-cn; SM-N9009 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) version/4.0 MQQBrowser/5.3 Mobile Safari/537.36 Mozilla/5.0 (Linux; Android 4.2.2; zh-cn; SCH-I959 Build/JDQ39) AppleWebKit/535.19 (KHTML, like Gecko) version/1.0 Chrome/18.0.1025.308 Mobile Safari/535.19 Mozilla/5.0 (Linux; U; Android 4.3; zh-CN; SM-N9009 Build/JSS15J) AppleWebKit/533.1 (KHTML, like Gecko) version/4.0 UCBrowser/9.9.2.467 U3/0.8.0 Mobile Safari/533.1 Mozilla/5.0 (Linux; U; Android 4.1.2; zh-CN; Coolpad 5891 Build/JZO54K) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/9.9.3.478 U3/0.8.0 Mobile Safari/533.1 Mozilla/5.0 (Linux; U; Android 4.1.2; zh-cn; Coolpad 5891 Build/JZO54K) appleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 running QQ/5.0.0.2215Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53 Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) mobile/11D257 QQ/5.0.0.165Mozilla/5.0 (Linux; Android 4.3; zh-cn; SAMSUNG-GT-I9308_TD/1.0 Android/4.3 Release/11.15.2013 Browser/AppleWebKit534.30 Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 Mozilla/5.0 (Linux; U; Android 4.1.1; zh-cn; GT-N7100 Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) version/4.0 Mobile Safari/534.30 SogouMSE, SogouMobileBrowser/3.2.3Mozilla/5.0 (Linux; U; Android 4.2.2; zh-cn; SCH-I959 Build/JDQ39) AppleWebKit/534.24 (KHTML, like Gecko) version/4.0 Mobile Safari/534.24 T5/2.0 baidubrowser/5.0.3.10 (Baidu; P1 4.2.2) Mozilla/5.0 (Linux; U; Android 4.2.2; zh-cn; SCH-I959 Build/JDQ39) appleWebKit/534.24 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.24 T5/2.0 Mozilla/5.0 (Linux; Android 4.3; SM-N9009 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.117 Mobile Safari/537.36 OPR/24.0.1565.82529Mozilla/5.0 (Linux; U; Android 4.4.4; zh-CN; Nexus 4 Build/KTU84P) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/9.9.2.467 U3/0.8.0 Mobile Safari/533.1 Mozilla/5.0 (Linux; U; Android 4.0.4; zh-cn; I C8825D Build appleWebKit/534.24 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.24 T5/2.0 baidubrowser/5.2.3.0 (Baidu; P1 4.0.4) Mozilla/5.0 (Linux; U; Android 4.0.4; zh-cn; C8825D Build/javasweic8825d) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 MQQBrowser/5.3 Mobile Safari/537.36 Mozilla/5.0 (Linux; Android 4.0.4; C8825D Build/javasweic8825d) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.117 Mobile Safari/537.36 Mozilla/5.0 (Linux; U; Android 4.0.4; zh-cn; C8825D Build/javasweic8825d) AppleWebKit/535.19 (KHTML, like Gecko) Version/4.0 LieBaoFast/2.12.0 Mobile Safari/535.19 Opera/9.80 (Android; Opera Mini/7.0.31907/34.2499; U; zh) Presto/2.8.119 Version/11.10 Mozilla/5.0 (Linux; U; Android 4.0.4; zh-cn; HW-_C8825D/C8825DV100R001C92B943SP01; 480*800; CTC/2.0) appleWebKit/534.30 Mobile Safari/534.30 Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; SGP521 Build/17.1.2.A.0.314) AppleWebKit/533.1 (KHTML, like Gecko) version/4.0 UCBrowser/9.9.2.467 U3/0.8.0 Mobile Safari/533.1 Mozilla/5.0 (Linux; Android 4.4.2; SGP521 Build/17.1.2.A.0.314) AppleWebKit/537.36 (KHTML, like Gecko) chrome/37.0.2062.117 Safari/537.36 Mozilla/5.0 (Linux; U; Android 4.0.4; zh-CN; C8825D Build/javasweic8825d) AppleWebKit/533.1 (KHTML, like Gecko) version/4.0 UCBrowser/9.8.5.442 U3/0.8.0 Mobile Safari/533.1 Mozilla/5.0 (Linux; U; Android 4.1.1; zh-cn; GT-N7100 Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 Mozilla/5.0 (Linux; Android 4.4.2; zh-cn; SAMSUNG-SM-N9009 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36 Mozilla/5.0 (Linux; U; Android 4.2.2; zh-CN; HTC HTL22 Build/JDQ39) AppleWebKit/533.1 (KHTML, like Gecko) version/4.0 UCBrowser/9.9.2.467 U3/0.8.0 Mobile Safari/533.1 Mozilla/5.0 (Linux; Android 4.3; SM-N9009 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) chrome/37.0.2062.117 Mobile Safari/537.36 Mozilla/5.0 (Linux; U; Android 4.2.1; zh-cn; AMOI A920W Build/JOP40D) AppleWebKit/534.30 (KHTML, like Gecko) version/4.0 Mobile Safari/534.30 Mozilla/5.0 (Linux; Android 4.3; SM-N9009 Build/JSS15J) AppleWebKit/537.36 (KHTML, like Gecko) chrome/36.0.1985.135 Mobile Safari/537.36 Mozilla/5.0 (Linux; U; Android 4.1.1; zh-CN; GT-N7100 Build/JRO03C) AppleWebKit/533.1 (KHTML, like Gecko) version/4.0 UCBrowser/9.9.2.467 U3/0.8.0 Mobile Safari/533.1 Mozilla/5.0 (Linux; U; Android 4.3; zh-cn; R8007 Build/JLS36C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 U8800 Android 2.3.3 Baidu 2.2 Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn) AppleWebKit/530.17 (KHTML, like Gecko) flyFlow/2.2 Version/4.0 Mobile Safari/530.17 useful (0) U8800 Android 2.3.3 UC 8.7 Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; U8800 Build/unzip weiu8800) UC AppleWebKit/534.31 (KHTML, like Gecko) Mobile Safari/534.31 useful (0) Meizu MX M031 Android 4.0.3 Chrome 18 Mozilla/5.0 (Linux; Android 4.0.3; M031 Build/IML74K) appleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19 useful (0) Meizu MX M031 Android 4.0.3 Opera 12.1 Opera/9.80 (Android 4.0.3; Linux; opera Mobi/ADR-1210241511) Presto/2.11.355 Version/12.10 useful (0) Meizu MX M031 Android 4.0.3-built-in * Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; m031 Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 useful (0) Meizu MX M031 Android 4.0.3 Baidu 2.2 Mozilla/5.0 (Linux; U; android 4.0.3; zh-cn) AppleWebKit/530.17 (KHTML, like Gecko) FlyFlow/2.2 Version/4.0 Mobile Safari/530.17 useful (0) meizu MX M031 Android 4.0.3 UC 8.7 Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; M031 Build/IML74K) UC AppleWebKit/534.31 (KHTML, like Gecko) mobile Safari/534.31 useful (0) Meizu M9 Android 4.0.3 QQ 3.7 MQQBrowser/3.7/Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; M9 Build/IML74K) appleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 Normal Mode useful (0) Meizu M9 Android 4.0.3 QQ 3.5 MQQBrowser/3.5/Adr (Linux; U; 4.0.3; zh-cn; M9 Build/Flyme 1.0.1; 640*960) Speed Mode useful (0) Meizu M9 Android 4.0.3-built-in * Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; M9 Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 useful (0) meizu M9 Android 4.0.3 QQ 3.5 MQQBrowser/3.5/Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; M9 Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) version/4.0 Mobile Safari/534.30 Normal Mode useful (0) Meizu MX M031 Android 4.0.3 Maxthon 2.7 Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; M031 Build/IML74K) appleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 useful (0) U8800 Android 2.3.3 Maxthon 2.7 Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; U8800 Build/javasweiu8800) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 useful (0) U8800 Android 2.3.3 QQ 3.7 MQQBrowser/3.7/Adr (Linux; U; 2.3.5; zh-cn; U8800 Build/U8800V100R001C00B528G002; 480*800) Speed Mode useful (0)  U8800 Android 2.3.3 doldolphin 9.1 Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; U8800 Build/unzip weiu8800) AppleWebKit/533.1 (KHTML, like Gecko) version/4.0 Mobile Safari/533.1 useful (0) U8800 Android 2.3.3 QQ 3.7 MQQBrowser/3.7/Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; u8800 Build/unzip weiu8800) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 Normal Mode useful (0) U8800 Android 2.3.3-built-in * Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; U8800 Build/unzip weiu8800) AppleWebKit/533.1 (KHTML, like Gecko) version/4.0 Mobile Safari/533.1 Samsung P6200 (GALAXY Tab) Android 3.2-built-in * Mozilla/5.0 (Linux; U; Android 3.2; zh-cn; GT-P6200 Build/HTJ85B) appleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13 useful (0) U8800 Android 2.3.3 Maxthon 4.0 Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; u8800 Build/unzip weiu8800) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 useful (0) Meizu MX M031 Android 4.0.3 Baidu 2.3 Mozilla/5.0 (Linux; U; android 4.0.3; zh-cn; M031 Build/IML74K) AppleWebKit/530.17 (KHTML, like Gecko) flyFlow/2.3 Version/4.0 Mobile Safari/530.17 baidubrowser/modules/uzieM_51_3.0.4_130M/g0a/modules % 7C408994110686468/1 useful (0) U8800 Android 2.3.3 Baidu 2.3 Mozilla/5.0 (Linux; u; Android 2.3.5; zh-cn; U8800 Build/javasweiu8800) AppleWebKit/530.17 (KHTML, like Gecko) flyFlow/2.3 Version/4.0 Mobile Safari/530.17 baidubrowser/modules/1001a/modules % 7C898293140340353/1 useful (0) U8800 Android 2.3.3 doldolphin 9.2 Mozilla/5.0 (Linux; u; Android 2.3.5; zh-cn; U8800 Build/javasweiu8800) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 useful (0) HTC S720e (One X) android 4.0.3-built-in * Mozilla/5.0 (Linux; U; Android 4.0.4; zh-cn; HTC S720e Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) version/4.0 Mobile Safari/534.30 useful (1) HTC S720e (One X) Android 4.0.3 UC 8.7 Mozilla/5.0 (Linux; U; Android 4.0.4; zh-cn; HTC S720e Build/IMM76D) UC AppleWebKit/534.31 (KHTML, like Gecko) Mobile Safari/534.31 useful (0) Meizu MX M031 Android 4.0.3 doldolphin Min 2.3 Mozilla/5.0 (Linux; U; android 4.0.3; zh-cn; M031 Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 useful (0) meizu MX M031 Android 4.0.3 QQ 4.0 MQQBrowser/4.0/Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; M031 Build/IML74K) AppleWebKit/533.1 (KHTML, like Gecko) mobile Safari/533.1 useful (0) Meizu M9 Android 4.0.3 QQ 3.7 MQQBrowser/3.7/Adr (Linux; U; 4.0.3; zh-cn; M9 Build/Flyme 1.0.1; 640*960) meizu MX2 M040 Android 4.1 UC 9.4 Mozilla/5.0 (Linux; U; Android 4.1.1; zh-CN; M040 Build/JRO03H) AppleWebKit/533.1 (KHTML, like Gecko) version/4.0 UCBrowser/9.4.1.uu3/0.8.0 Mobile Safari/533.1 useful (0) Meizu MX2 M040 Android 4.1 Chrome 31 Mozilla/5.0 (Linux; Android 4.1.1; M040 Build/JRO03H) appleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36 useful (0) Meizu MX2 M040 Android 4.1 cheetah 2.8 Mozilla/5.0 (Linux; Android 4.1.1; m040 Build/JRO03H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.64 Mobile Safari/537.36 useful (0) Meizu MX2 M040 Android 4.1 Baidu 4.1 Mozilla/5.0 (Linux; U; android 4.1.1; zh-cn; M040 Build/JRO03H) AppleWebKit/534.24 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.24 T5/2.0 baidubrowser/4.2.4.0 (Baidu; P1 4.1.1) useful (0) Meizu MX M031 Android 4.1-built-in * Mozilla/5.0 (Linux; U; Android 4.1.1; zh-cn; M031 Build/JRO03H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 useful (1) Meizu MX M031 Android 4.1 UC 8.8 Mozilla/5.0 (Linux; U; Android 4.1.1; zh-CN; m031 Build/JRO03H) AppleWebKit/534.31 (KHTML, like Gecko) UCBrowser/8.8.3.278 U3/0.8.0 Mobile Safari/534.31 useful (0) meizu MX2 M040 Android 4.1 QQ 4.1 Mozilla/5.0 (Linux; U; Android 4.1.1; zh-cn; M040 Build/JRO03H) AppleWebKit/533.1 (KHTML, like Gecko) version/4.0 MQQBrowser/4.1 Mobile Safari/533.1 useful (0) Meizu MX2 M040 Android 4.1-built-in * Mozilla/5.0 (Linux; U; Android 4.1.1; zh-cn; m040 Build/JRO03H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 useful (2) Samsung P6200 (GALAXY Tab) android 3.2 qq hd 2.1 Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, like Gecko) version/5.0 Safari/533.16 useful (0) Samsung P6200 (GALAXY Tab) Android 3.2 uc hd 2.3 Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3) appleWebKit/534.31 (KHTML, like Gecko) Chrome/17.0.558.0 Safari/534.31 UCBrowser/2.3.1.257

After verification, the success rate is over 95%.


Solution 2: open-source library WURFL


Address: http://wurfl.sourceforge.net/apis.php

Online Test address: http://tools.scientiamobile.com/



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.