一、UserAgentUtils
UserAgentUtils是一个处理user-agent 字符的一个工具。可以用来实时地处理http请求和分析http请求日志文件。这里可以使用UserAgentUtils对访问用户的浏览器类型、操作系统、设备种类等进行统计分析,从而为网址的用户量的增加,做出正确的决策。项目网址为:https://github.com/HaraldWalker/user-agent-utils
示例相关相关maven依赖如下:
<dependency> <groupId>nl.bitwalker</groupId> <artifactId>UserAgentUtils</artifactId> <version>1.2.4</version> </dependency>
二、示例
相关实例代码如下:
1 package com.sunshine.test; 2 3 import org.junit.Test; 4 5 import nl.bitwalker.useragentutils.Browser; 6 import nl.bitwalker.useragentutils.UserAgent; 7 import nl.bitwalker.useragentutils.Version; 8 9 public class TestUserAgent { 10 11 /** 12 * 13 * @param userAgentStr 14 * 用户访问的userAgent 字符串 15 */ 16 private static void getType(String userAgentStr) { 17 UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr); 18 Browser browser = userAgent.getBrowser(); 19 20 String browserName = browser.getName();// 浏览器名称 21 String group = browser.getGroup().getName();// 浏览器大类 22 Version browserVersion = userAgent.getBrowserVersion();// 详细版本 23 String version = browserVersion.getMajorVersion();// 浏览器主版本 24 System.out.println(browserName); 25 System.out.println(group); 26 System.out.println(browserVersion); 27 System.out.println(version); 28 System.out.println(userAgent.getOperatingSystem());// 访问设备系统 29 System.out.println(userAgent.getOperatingSystem().getDeviceType());// 访问设备类型 30 System.out.println(userAgent.getOperatingSystem().getManufacturer());// 访问设备制造厂商 31 32 33 System.out.println(String.format("===============%s-%s", group, version)); 34 } 35 36 @Test 37 public void test() { 38 String userAgent = "user-agent = Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0"; 39 String userAgent2 = "user-agent = Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36"; 40 String userAgent3 = "user-agent = Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; 41 String userAgent4 = "user-agent = Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B440 Safari/600.1.4"; 42 43 getType(userAgent); 44 getType(userAgent2); 45 getType(userAgent3); 46 getType(userAgent4); 47 } 48 }
运行结果如下: