赞
踩
一些网站常常通过判断 UA 来给不同的操作系统、不同的浏览器发送不同的页面,因此可能造成某些页面无法在某个浏览器中正常显示,但通过伪装 UA 可以绕过检测。
一:获得浏览器请求头中的User-Agent
String ua = request.getHeader("User-Agent")
- UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
- Browser browser = userAgent.getBrowser();
- OperatingSystem os = userAgent.getOperatingSystem();
方案一:正则表达式
通过观察规律,得出以下表达式:
;\s?([^;]+?)\s?(Build)?/
- 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("通过userAgent解析出机型:" + model);
- }
-
- 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.2215
- Mozilla/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; C
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。