zoukankan      html  css  js  c++  java
  • JavaScript获取客户端IP地址

    1. 第三方接口

    1) 这里提供一个搜狐接口的地址:http://pv.sohu.com/cityjson?ie=utf-8 ,将这个js引入到页面即可得到returnCitySN。

    2) api.ipify.org

    https://api.ipify.org/?format=jsonp&callback=getIP

    1 <script type="application/javascript">
    2   function getIP(json) {
    3     document.write("My public IP address is: ", json.ip);
    4   }
    5 </script>
    6 
    7 <script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>

    或者

    1 $.getJSON('https://api.ipify.org?format=json', function(data){
    2     console.log(data.ip);
    3 });

    2. 向服务器发送一个ajax请求,该请求包中会包含客户端的相关信息,当然也包括IP。

    3. 使用webRTC,获取私有IP

    The RTCPeerConnection() constructor returns a newly-created RTCPeerConnection, which represents a connection between the local device and a remote peer.(http://ourcodeworld.com/articles/read/257/how-to-get-the-client-ip-address-with-javascript-only)

     1 /**
     2  * Get the user IP throught the webkitRTCPeerConnection
     3  * @param onNewIP {Function} listener function to expose the IP locally
     4  * @return undefined
     5  */
     6 function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
     7     //compatibility for firefox and chrome
     8     var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
     9     var pc = new myPeerConnection({
    10         iceServers: []
    11     }),
    12     noop = function() {},
    13     localIPs = {},
    14     ipRegex = /([0-9]{1,3}(.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
    15     key;
    16 
    17     function iterateIP(ip) {
    18         if (!localIPs[ip]) onNewIP(ip);
    19         localIPs[ip] = true;
    20     }
    21 
    22      //create a bogus data channel
    23     pc.createDataChannel("");
    24 
    25     // create offer and set local description
    26     pc.createOffer().then(function(sdp) {
    27         sdp.sdp.split('
    ').forEach(function(line) {
    28             if (line.indexOf('candidate') < 0) return;
    29             line.match(ipRegex).forEach(iterateIP);
    30         });
    31         
    32         pc.setLocalDescription(sdp, noop, noop);
    33     }).catch(function(reason) {
    34         // An error occurred, so handle the failure to connect
    35     });
    36 
    37     //listen for candidate events
    38     pc.onicecandidate = function(ice) {
    39         if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
    40         ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
    41     };
    42 }
    43 
    44 // Usage
    45 
    46 getUserIP(function(ip){
    47     alert("Got IP! :" + ip);
    48 });
  • 相关阅读:
    (数据科学学习手札09)系统聚类算法Python与R的比较
    写完代码就去吃饺子|The 10th Henan Polytechnic University Programming Contest
    《四月物语》
    PAT(甲级)2017年春季考试
    PAT(甲级)2017年秋季考试
    2019年全国高校计算机能力挑战赛 C语言程序设计决赛
    CF#603 Div2
    redhat7 上安装dummynet
    cassandra 如何写数据以及放置副本
    Eclipse中设置VM参数
  • 原文地址:https://www.cnblogs.com/fangsmile/p/7307463.html
Copyright © 2011-2022 走看看