获取请求头中User-Agent工具类
Posted gabriel-y
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取请求头中User-Agent工具类相关的知识,希望对你有一定的参考价值。
public class AgentUserKit { private static String pattern = "^Mozilla/\d\.\d\s+\(+.+?\)"; private static String pattern2 = "\(+.+?\)"; private static Pattern r = Pattern.compile(pattern); private static Pattern r2 = Pattern.compile(pattern2); public static String getDeviceInfo(HttpServletRequest request) { String userAgent = request.getHeader("User-Agent"); if (userAgent==null){ return null; } return getDeviceInfo(userAgent); } private static String getDeviceInfo(String userAgent) { String result=null; try { Matcher m = r.matcher(userAgent); if (m.find()) { result = m.group(0); } Matcher m2 = r2.matcher(result); if (m2.find()) { result = m2.group(0); } result = result.replace("(", ""); result = result.replace(")", ""); } catch (Exception e) { return userAgent; } return filterDeviceInfo(result); } public static String filterDeviceInfo(String result) { if (StringUtils.isEmpty(result)) { return null; } result = result.replace(" U;", ""); result = result.replace(" zh-cn;", ""); return result; } }
以上是关于获取请求头中User-Agent工具类的主要内容,如果未能解决你的问题,请参考以下文章