HttpServletRequest#getHeader(“User-Agent”) returns null browser name

拈花ヽ惹草 提交于 2019-12-05 03:24:44
Ryan Stewart

String browserName = requestProvider.get().getHeader("User-Agent");

null means whoever sent the request didn't include a "User-Agent" header.

String ipAdd = requestProvider.get().getRemoteAddr();

is unlikely to return null under normal circumstances, but there are reports the it may do so in edge cases, like after the response has already been sent. Regardless, "get IP address of the machine on which my application is running" doesn't sound like what getRemoteAddr() is for. It's for getting the address of the most recent client or proxy that sent the request.

Is there any alternate way to get IP address & browser name every time?

No. You're entirely dependent on the behavior of the HTTP client and/or any intervening proxies to get information like this.

Try using user-agent as lowercase, because it works if we directly access from header.

String browserName = requestProvider.get().getHeader("user-agent");

alternate way to get IP address is

String ip = requestProvider.get().getHeader("True-Client-IP"); this works if we have akamai integeration.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!