使用javascript获取客户端的本地IP地址

时间:2012-12-18 13:10:42

标签: javascript .net ip

  

可能重复:
  Get Client IP using just Javascript?

如何检索/获取客户端IP地址?

1 个答案:

答案 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>

您也可以参考How to display client IP Address