问题
I'm working on a Spring MVC application, and I need to access client browser name and version.
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.
I need to exact number and version. Is there any tools for doing that?
回答1:
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
source is available at
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());
回答2:
Useful library for parsing the result of User-Agent Http Header : browscap-java
来源:https://stackoverflow.com/questions/28323058/how-to-get-the-exact-client-browser-name-and-version-in-spring-mvc