简体   繁体   English

HttpServletRequest#getHeader(“User-Agent”) 返回空浏览器名称

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

I'm using Java 6. I have very less knowledge of JSP and Servlets.我使用的是 Java 6。我对 JSP 和 Servlet 的了解很少。

I'm using the following code to get browser name in which my application is running:我正在使用以下代码获取运行我的应用程序的浏览器名称:

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

Also I'm using the following code to get IP address of the machine on which my application is running:此外,我使用以下代码获取运行我的应用程序的机器的 IP 地址:

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

In both the cases requestProvider is a reference variable of type Provider<HttpServletRequest> .在这两种情况下requestProvider都是Provider<HttpServletRequest>类型的引用变量。 And I'm assured that it is never NULL .而且我确信它永远不会NULL

Now the problem is some times I get both values ( browserName and ipAdd ) NULL.现在问题是有时我得到两个值( browserName and ipAdd )NULL。 I've written sometimes because I don't have a test case.我有时会写,因为我没有测试用例。

So my question is, what are the cases in Java, when these values can be NULL?所以我的问题是,当这些值可以为 NULL 时,Java 中的情况是什么?

What care should I take in coding to avoid this issue?为了避免这个问题,我在编码时应该注意什么?

Is there any alternate way to get IP address & browser name every time?是否有任何其他方法可以每次获取 IP 地址和浏览器名称?

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

null means whoever sent the request didn't include a"User-Agent" header . null表示发送请求的人不包含“User-Agent”标头

String ipAdd = requestProvider.get().getRemoteAddr(); 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 .在正常情况下不太可能返回 null,但有报告说它可能会在边缘情况下返回,比如在响应已经发送之后 Regardless, "get IP address of the machine on which my application is running" doesn't sound like what getRemoteAddr() is for.无论如何,“获取运行我的应用程序的机器的 IP 地址”听起来不像getRemoteAddr()的用途。 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?是否有任何其他方法可以每次获取 IP 地址和浏览器名称?

No. You're entirely dependent on the behavior of the HTTP client and/or any intervening proxies to get information like this.不。您完全依赖于 HTTP 客户端和/或任何干预代理的行为来获取此类信息。

Try using user-agent as lowercase, because it works if we directly access from header.尝试使用小写的用户代理,因为如果我们直接从头访问它就可以工作。

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

alternate way to get IP address is获取IP地址的替代方法是

String ip = requestProvider.get().getHeader("True-Client-IP"); String ip = requestProvider.get().getHeader("True-Client-IP"); this works if we have akamai integeration.如果我们有 akamai 整数化,这会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM