Java gets browser type, OS type, phone model via browser request header (user-agent)

Source: Internet
Author: User

The user Agent Chinese name is called "UA", which is a special string header that allows the server to identify the operating system and version, CPU type, browser and version, browser rendering engine, browser language, browser plugin, etc. used by the client.

Some websites often judge the UA to send different pages to different operating systems, different browsers, which may cause some pages to not display properly in a browser, but can bypass detection by disguising the UA.

One: Get user-agent in the browser request header

String ua = Request.getheader ("User-agent")

Two: Get browser type, operating system type: (note, useragent class in Useragentutils.jar, self-download)

    UserAgent useragent = useragent.parseuseragentstring (Request.getheader ("user-agent"));             Browser Browser = Useragent.getbrowser ();             OperatingSystem OS = Useragent.getoperatingsystem ();  

Three: Get phone type:


Scenario One: 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 ("model is resolved by useragent:" + model);                  }  

The following is a partial useragent for testing, which can be verified directly in the 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.30mozilla/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.36mozilla/ 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.19mozilla/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.1mozilla/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/5 33.1mozilla/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 v1_and_sq_5.0.0_146_yyb2_d Q q/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.53mozil la/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.30mozilla/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.0mozilla/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.1m ozilla/5.0 (Linux; U Android 4.0.4; ZH-CN; HUAWEI c8825d build/huaweic8825d) applewebkit/534.24 (khtml, like Gecko) version/4.0 Mobile safari/534.24 t5/2.0 Baidubro wser/5.2.3.0 (Baidu; P1 4.0.4) mozilla/5.0 (Linux; U Android 4.0.4; ZH-CN; HUAWEI c8825d build/huaweic8825d) applewebkit/537.36 (khtml, like Gecko) version/4.0 mqqbrowser/5.3 Mobile safari/ 537.36mozilla/5.0 (Linux; Android 4.0.4; HUAWEI c8825d build/huaweic8825d) applewebkit/537.36 (khtml, like Gecko) chrome/37.0.2062.117 Mobile safari/ 537.36mozilla/5.0 (Linux; U Android 4.0.4; ZH-CN; HUAWEI c8825d Build/huaweic8825d) applewebkit/535.19 (khtml, like Gecko) version/4.0 liebaofast/2.12.0 Mobile safari/535.19opera/9.80 ( Android; Opera mini/7.0.31907/34.2499; U ZH) presto/2.8.119 version/11.10mozilla/5.0 (Linux; U Android 4.0.4; ZH-CN; HW-HUAWEI_C8825D/C8825DV100R001C92B943SP01; 480*800; ctc/2.0) applewebkit/534.30 Mobile safari/534.30mozilla/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.1mozilla/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.36mozilla/5.0 ( Linux; U Android 4.0.4; ZH-CN; HUAWEI c8825d build/huaweic8825d) applewebkit/533.1 (khtml, like Gecko) version/4.0 ucbrowser/9.8.5.442 U3/0.8.0 Mobile S afari/533.1mozilla/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.30mozilla/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.36mozilla/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.36mozilla/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.30mozilla/5.0 (Linux; Android 4.3; sm-n9009 build/jss15j) applewebkit/537.36 (khtml, like Gecko) chrome/36.0.1985.135 Mobile safari/537.36mozilla/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.1mozilla/5.0 (Linux; U Android 4.3; ZH-CN; R8007 build/jls36c) applewebkit/534.30 (khtml, like Gecko) version/4.0 Mobile safari/534.30Huawei 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) Huawei U8800 Android 2.3.3 UC 8.7 mozilla/5.0 (Linux; U Android 2.3.5; ZH-CN; U8800 build/huaweiu8800) UC applewebkit/534.31 (khtml, like Gecko) Mobile safari/534.31 Useful (0) Meizu MX M031 Android 4.0.3 C Hrome 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 and Roid 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 mqqbr owser/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 an Droid 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) Huawei U8800 Android 2.3.3 Maxthon 2.7 mozilla/5.0 (Linux; U Android 2.3.5; ZH-CN; U8800 build/huaweiu8800) applewebkit/533.1 (khtml, like Gecko) version/4.0 Mobile safari/533.1 Useful (0) Huawei 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) Huawei U8800 Android 2.3.3 Dolphin 9.1 mozilla/5.0 (Linux; U ; Android 2.3.5; ZH-CN; U8800 build/huaweiu8800) applewebkit/533.1 (khtml, like Gecko) version/4.0 Mobile safari/533.1 Useful (0) Huawei U8800 Android 2 .3.3 QQ 3.7 mqqbrowser/3.7/mozilla/5.0 (Linux; U Android 2.3.5; ZH-CN; U8800 build/huaweiu8800) applewebkit/533.1 (khtml, like Gecko) version/4.0 Mobile safari/533.1 Normal Mode Useful (0) Huawei U88 XX Android 2.3.3-built-in * mozilla/5.0 (Linux; U Android 2.3.5; ZH-CN; U8800 build/huaweiu8800) applewebkit/533.1 (khtml, like Gecko) version/4.0 Mobile Safari/533.1samsung P6200 (GALAXY Tab) A Ndroid 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) Huawei U8800 Android 2.3.3 Ma Xthon 4.0 mozilla/5.0 (Linux; U Android 2.3.5; ZH-CN; U8800 build/huaweiu8800) 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/023_ 1.41.3.2_DIORDNA_069_046/UZIEM_51_3.0.4_130M/1200A/963E77C7DAC3FA587DF3A7798517939D%7C408994110686468/1 Useful (0) Huawei U8800 Android 2.3.3 Baidu 2.3 mozilla/5.0 (Linux; U Android 2.3.5; ZH-CN; U8800 build/huaweiu8800) applewebkit/530.17 (khtml, like Gecko) flyflow/2.3 version/4.0 Mobile safari/530.17 baidubrowser/042_1.6.3.2_diordna_008_084/iewauh_01_5.3.2_0088u/1001a/be44df7faba8768b2a1b1e93c4bad478% 7C898293140340353/1 Useful (0) Huawei U8800 Android 2.3.3 Dolphin 9.2 mozilla/5.0 (Linux; U AnDroid 2.3.5; ZH-CN; U8800 build/huaweiu8800) applewebkit/533.1 (khtml, like Gecko) version/4.0 Mobile safari/533.1 Useful (0) HTC s720e (one X) Andro ID 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 OID 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 do Lphin 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.362 u3/0.8.0 Mobile safari/533.1 Useful ( 0) Meizu MX2 M040 Android 4.1 Chrome 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 and Roid 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 and Roid 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 (B Aidu; 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 Q Q 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 M04 0 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

Through verification, the success rate is more than 95%.

Scenario Two: Open Source class Library WURFL

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

Online test Address: http://tools.scientiamobile.com/


Java gets browser type, OS type, phone model via browser request header (user-agent)

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.