java 如何在 Spring MVC 中获取确切的客户端浏览器名称和版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28323058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the exact client browser name and version in Spring MVC?
提问by hamed
I'm working on a Spring MVC application, and I need to access client browser name and version.
我正在开发一个 Spring MVC 应用程序,我需要访问客户端浏览器名称和版本。
I have an instance of HttpServletRequest
in my action as a parameter and use request.getHeader("User-Agent")
method, but this returned Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
in Internet explorer 9.
我HttpServletRequest
在我的操作中有一个实例作为参数和使用request.getHeader("User-Agent")
方法,但这Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
在 Internet Explorer 9 中返回。
I need to exact number and version. Is there any tools for doing that?
我需要确切的数字和版本。是否有任何工具可以做到这一点?
回答by Master Slave
Acknowledging that the user agent is unsafe. Still, in the lack of other ways, you should parse a user-agent header, which in fact is not as easy, as the number of combinations is overwhelming. Unless you want to roll your own, I would suggest
承认用户代理是不安全的。尽管如此,在缺乏其他方法的情况下,您应该解析用户代理标头,这实际上并不容易,因为组合的数量太多了。除非你想自己动手,否则我建议
http://www.bitwalker.eu/software/user-agent-utils
http://www.bitwalker.eu/software/user-agent-utils
source is available at
来源可在
https://github.com/HaraldWalker/user-agent-utils
https://github.com/HaraldWalker/user-agent-utils
the usage is quite straightforward
用法很简单
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
System.out.println(userAgent.getBrowser().getName() + " " + userAgent.getBrowserVersion());
回答by A. Masson
Useful library for parsing the result of User-Agent
Http Header : browscap-java
用于解析User-Agent
Http Header结果的有用库:browscap-java