1
votes

I am trying to get the information about which browser the user used to call the API. I searched online and it seems that I should use the "user-agent" from the request header.

Code:

@RequestMapping(value = "/headerTest", method = RequestMethod.GET)
public @ResponseBody DummyAPI dummyApi(
        @RequestHeader ("User-Agent") String userAgent,
        HttpServletRequest request,  HttpServletResponse response)
{

    System.out.println("User-Agent : " + userAgent);

    String browserName = request.getHeader("User-Agent");
    System.out.println("BrowserName : " + browserName);

    return "test";
}

However, when I test it:

Firefox gives me: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0!

Chrome gives me: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36!

Safari gives me: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.76.4 (KHTML, like Gecko) Version/7.0.4 Safari/537.76.4!

What could be the problem here? Why would the header be wrong?

Thank you in advance!

1
Did you run some addon that changes your user-agent to spoof it, and forgot you did it? User-agent is sent by the browser. If Java's getting it wrong, its because the browser sent it wrong.developerwjk
I think these user agent strings are true, I checked mine on whatsmyuseragent.com (I am using Chrome 35 on Windows 8.1) It seems like my user agent is very similar to your Chrome user agent (except OS part) string Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36. So, don't get confused by all these Mozilla-Safari words etc.Utku Özdemir
This is correct, the user agent will provide you information about the browser which includes much more than just the name of the browser. You can search this user agent string to check for the browser names you are looking for using something like userAgent.contains("Firefox").Chirag Bhatia - chirag64

1 Answers

2
votes

The headers are correct and there's nothing wrong with them or your application. For historic reasons they contain a lot data and can't be easily changed to be compatible with old web applications.

source and more info: http://webaim.org/blog/user-agent-string-history/