h5获取ip地址

一、获取公网ip

1、用新浪搜狐腾讯jS接口取客户端IP及省份城市

新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js

新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42

搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson

搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8

搜狐另外的IP地址查询接口:http://txt.go.sohu.com/ip/soip

腾讯的接口:http://fw.qq.com/ipaddress (腾讯的接口原本是最好用的,可现在打不开啦)

上面的接口中新浪的是js接口说明:

<script type="text/javascript"src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js" charset="gb2312"></script>
<script type="text/javascript">
    function ipmsg() {
        var ss = remote_ip_info['country']  + remote_ip_info['province']+remote_ip_info['city'];
        document.getElementByIdx_x_x("<%=HiddenField1.ClientID %>").value = ss;
  }
</script>

js获取ip地址:

<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>//引用JavaScript地址接口
<script type="text/javascript">
     document.write(returnCitySN["cip"] + ',' + returnCitySN["cname"]) //打印截取指定的字段值
</script>

这个是最简单的

<script type="text/javascript" src="http://ip.chinaz.com/getip.aspx "></script> 

2、ip138查询

$.ajax({
                url: 'http://api.ip138.com/query/',
                data: {            //默认自动添加callback参数
                    oid: '9349',
                    mid: '72219',
                    token: '1812c7e62bfd7b8f2f1bfe2054a805dc'  //不安全,请定期刷新token,建议进行文件压缩
                },
                dataType: 'jsonp',
                success: function (json) {
                    console.log(json);
                }
            });

二、获取内网ip

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <ul><li>内网ip</li></ul>
        <script>
            function getIPs(callback) {
                var ip_dups = {};
                var RTCPeerConnection = window.RTCPeerConnection ||
                    window.mozRTCPeerConnection ||
                    window.webkitRTCPeerConnection;
                var useWebKit = !!window.webkitRTCPeerConnection;
                if (!RTCPeerConnection) {
                    var iframe = document.createElement('iframe');
                    iframe.style.display = 'none';
                    iframe.sandbox = 'allow-same-origin';
                    iframe.addEventListener("DOMNodeInserted", function(e) {
                        e.stopPropagation();
                    }, false);
                    iframe.addEventListener("DOMNodeInsertedIntoDocument", function(e) {
                        e.stopPropagation();
                    }, false);

                    document.body.appendChild(iframe);
                    var win = iframe.contentWindow;
                    RTCPeerConnection = win.RTCPeerConnection ||
                        win.mozRTCPeerConnection ||
                        win.webkitRTCPeerConnection;
                    useWebKit = !!win.webkitRTCPeerConnection;
                }

                var mediaConstraints = {
                    optional: [{
                        RtpDataChannels: true
                    }]
                };

                var servers = undefined;
                if (useWebKit)
                    servers = {
                        iceServers: [{
                            urls: "stun:stun.services.mozilla.com"
                        }]
                    };
                var pc = new RTCPeerConnection(servers, mediaConstraints);
                function handleCandidate(candidate) {
                    var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
                    var ip_addr = ip_regex.exec(candidate)[1];
                    if (ip_dups[ip_addr] === undefined)
                        callback(ip_addr);
                    ip_dups[ip_addr] = true;
                }

                pc.onicecandidate = function(ice) {
                    if (ice.candidate)
                        handleCandidate(ice.candidate.candidate);
                };

                pc.createDataChannel("");
                pc.createOffer(function(result) {
                    pc.setLocalDescription(result, function() {}, function() {});
                }, function() {});

                setTimeout(function() {
                    var lines = pc.localDescription.sdp.split('\n');
                    lines.forEach(function(line) {
                        if (line.indexOf('a=candidate:') === 0)
                            handleCandidate(line);
                    });
                }, 1000);
            }

            getIPs(function(ip) {
                var li = document.createElement("li");
                li.textContent = ip;
                if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) {
                    //内网ip
                    document.getElementsByTagName("ul")[0].appendChild(li);
                }
            });
        </script>
    </body>
</html>

 

转载于:

https://blog.csdn.net/enweitech/article/details/52084346

https://blog.csdn.net/weixin_48730699/article/details/119635259

posted @ 2022-09-06 10:05  阳光下的向日葵  阅读(2359)  评论(0编辑  收藏  举报