如何检索/获取客户端IP地址?
答案 0 :(得分:3)
如果您需要服务器端脚本,我的意思是 Java
代码,请参阅此
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class IPAddress{
public static void main(String[] a) {
try {
InetAddress thisIp = InetAddress.getLocalHost();
System.out.print(thisIp.getHostAddress());
} catch (UnknownHostException ex) {
Logger.getLogger(study.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
<强>更新强>
如你所说,你需要javaScript
。请参考以下代码并告知我们。
<html>
<head>
<script type="text/javascript" language="javascript">
function myIP() {
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","http://jsonip.appspot.com/",false);
xmlhttp.send();
hostipInfo = xmlhttp.responseText;
obj = JSON.parse(hostipInfo);
document.getElementById("IP").value=obj.ip;
document.getElementById("ADDRESS").value=obj.address;
}
</script>
</head>
<body onload="myIP()">
IP :<input type="text" id="IP" name="IP" />
ADDRESS :<input type="text" id="ADDRESS" name="ADDRESS" />
</body>
</html>