1
我知道這個問題在很多次之前被提出,但是我的問題是不同的。 我想檢測可以在世界任何地方的客戶端的IP地址。我用Google搜索了一下,但沒有成功。尋找ip地址的代碼總是返回null
當我問我們的網絡管理員時,他告訴我,我們的代理服務器的IP爲192.xxx.xxx.xxx,而對於我們公司網絡的外部,IP是180.xxx.xxx.xxx,它與我們的代理服務器的IP地址[192.xxx.xxx.xxx]
這是我試過的代碼。這段代碼駐留在servlet中。 所有返回null。 在此先感謝...
String ipAddress;
ipAddress = request.getHeader("X-FORWARDED-FOR");
System.out.println(ipAddress);
if (ipAddress == null) {
ipAddress = request.getRemoteAddr();
System.out.println(ipAddress);
}
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
System.out.println(ipAddress);
ipAddress = request.getHeader("X-Forwarded-For");
System.out.println(ipAddress);
ipAddress = request.getHeader("Proxy-Client-IP");
System.out.println(ipAddress);
ipAddress = request.getHeader("WL-Proxy-Client-IP");
System.out.println(ipAddress);
ipAddress = request.getHeader("HTTP_CLIENT_IP");
System.out.println(ipAddress);
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
System.out.println(ipAddress);
http://stackoverflow.com/questions/18350318/what-is-the-right-way-to-get-requests-ip –
的只有一人能夠可靠地回答這個問題是管理員你公司代理。但是你應該告訴他爲什麼你需要這些信息,並問他可以做些什麼。 –
你是從localhost運行的嗎? – christopher