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!
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 ÖzdemiruserAgent.contains("Firefox")
. – Chirag Bhatia - chirag64