How to get the exact client browser name and version in Spring MVC?

拈花ヽ惹草 提交于 2019-12-18 11:29:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!