You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过JavaScript代码获取IP地址?

使用JavaScript获取客户端IP地址

下面是利用WebRTC的RTCPeerConnection接口获取客户端IP地址的具体实现代码:

const peerConnection = new RTCPeerConnection({ iceServers: [] });
peerConnection.createDataChannel('');

peerConnection.createOffer()
  .then((offer) => peerConnection.setLocalDescription(offer))
  .catch((error) => console.log(error));

peerConnection.onicecandidate = (event) => {
  if (event.candidate) {
    console.log(`Client's IP address is ${event.candidate.address}`);
  }
};

简单说明

  • 代码通过创建空的RTCPeerConnection实例触发ICE候选收集流程,当ICE候选对象生成时,即可从中提取出客户端的IP地址。
  • 该方法在部分浏览器隐私限制场景下可能无法正常获取IP。

内容的提问来源于stack exchange,提问作者Gopal k .Dwivedi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 22:32:01