hamed
hamed

Reputation: 8033

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

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?

Upvotes: 15

Views: 29560

Answers (2)

A. Masson
A. Masson

Reputation: 2477

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

Upvotes: 3

Master Slave
Master Slave

Reputation: 28569

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());

Upvotes: 23

Related Questions