java獲取客戶端的ip
⑴ 請教java獲取客戶端真實IP最好的方法
java獲取客戶端真實IP最好的方法
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
⑵ JAVA如何獲取客戶端IP地址和MAC地址
/**
* 獲取IP地址
* @return
*/
public static String GetNetIp() {
URL infoUrl = null;
InputStream inStream = null;
String line = "";
try {
infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inStream = httpConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
StringBuilder strber = new StringBuilder();
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
inStream.close();
// 從反饋的結果中提取出IP地址
int start = strber.indexOf("{");
int end = strber.indexOf("}");
String json = strber.substring(start, end + 1);
if (json != null) {
try {
JSONObject jsonObject = new JSONObject(json);
line = jsonObject.optString("cip");
} catch (JSONException e) {
e.printStackTrace();
}
}
return line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
public static String getLocalMacAddress() {//沒有緩存的地址,則查詢
String mac_s = ""; try { byte[] mac;
NetworkInterface ne = NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
mac = ne.getHardwareAddress();
mac_s = byte2hex(mac);
} catch (Exception e) {
} mac_s; return mac_s;
}
⑶ java如何獲取https協議的客戶端ip地址
嗨 你好
據網上了解到:
在JSP里,獲取客戶端的IP地址的方法是:request.getRemoteAddr(),這種方法在大部分情況下都是有效的。但是在通過了Apache,Squid等反向代理軟體就不能獲取到客戶端的真實IP地址了。
如果使用了反向代理軟體,將http://192.168.1.110:2046/ 的URL反向代理為 http://www.javapeixun.com.cn / 的URL時,用request.getRemoteAddr()方法獲取的IP地址是:127.0.0.1或192.168.1.110,而並不是客戶端的真實IP。
經過代理以後,由於在客戶端和服務之間增加了中間層,因此伺服器無法直接拿到客戶端的IP,伺服器端應用也無法直接通過轉發請求的地址返回給客戶端。但是在轉發請求的HTTP頭信息中,增加了X-FORWARDED-FOR信息。用以跟蹤原有的客戶端IP地址和原來客戶端請求的伺服器地址。當我們訪問http://www.javapeixun.com.cn /index.jsp/ 時,其實並不是我們瀏覽器真正訪問到了伺服器上的index.jsp文件,而是先由代理伺服器去訪問http://192.168.1.110:2046/index.jsp ,代理伺服器再將訪問到的結果返回給我們的瀏覽器,因為是代理伺服器去訪問index.jsp的,所以index.jsp中通過request.getRemoteAddr()的方法獲取的IP實際上是代理伺服器的地址,並不是客戶端的IP地址。
於是可得出獲得客戶端真實IP地址的方法一:
public String getRemortIP(HttpServletRequest request) { if (request.getHeader("x-forwarded-for") == null) { return request.getRemoteAddr(); } return request.getHeader("x-forwarded-for"); }
可是當我訪問http://www.5a520.cn /index.jsp/ 時,返回的IP地址始終是unknown,也並不是如上所示的127.0.0.1或192.168.1.110了,而我訪問http://192.168.1.110:2046/index.jsp 時,則能返回客戶端的真實IP地址,寫了個方法去驗證。原因出在了Squid上。squid.conf 的配製文件forwarded_for 項默認是為on,如果 forwarded_for 設成了 off 則:X-Forwarded-For: unknown
於是可得出獲得客戶端真實IP地址的方法二:
public String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; }
可是,如果通過了多級反向代理的話,X-Forwarded-For的值並不止一個,而是一串Ip值,究竟哪個才是真正的用戶端的真實IP呢?
答案是取X-Forwarded-For中第一個非unknown的有效IP字元串。
如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100用戶真實IP為: 192.168.1.110
希望可以幫到你的忙
祝你學習愉快
⑷ java怎麼獲取本地ip地址
Jva獲取本地的IP地址,這個你就需要到你的手機的網路客戶端裡面進行設置了,這個話都是可以去查看的。