15

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?

nbro
  • 15,395
  • 32
  • 113
  • 196
hamed
  • 7,939
  • 15
  • 60
  • 114
  • There is no safe way to do it. The browser name and version comes from the browser (the _client_). It can be easily spoofed, so you can't be 100% sure to have the correct name and version of the browser – BackSlash Feb 04 '15 at 13:55

2 Answers2

23

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());
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
Master Slave
  • 27,771
  • 4
  • 57
  • 55
3

Useful library for parsing the result of User-Agent Http Header : browscap-java

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
A. Masson
  • 2,287
  • 3
  • 30
  • 36