13

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
  • 13,796
  • 25
  • 99
  • 185
hamed
  • 7,681
  • 13
  • 50
  • 104
  • 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

20

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
  • 13,649
  • 17
  • 81
  • 128
Master Slave
  • 26,268
  • 4
  • 56
  • 55
1

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

Manuel Jordan
  • 13,649
  • 17
  • 81
  • 128
A. Masson
  • 2,037
  • 3
  • 27
  • 34