使用http请求ifconfig.co获取与Java中私有IP相关联的公共IP
我编写了一个代码,以获取与每个接口相关联的所有私人IP。我正在将所有私人IP存储在arraylist ips
中。
List<String> ips = new ArrayList<>();
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
ips.add(enumIpAddr.nextElement().toString().replace("/", ""));
}
}
现在,我想在列表中迭代IPS,并想调用HTTP请求以获取公共IP。以下是Linux Curl命令,它将返回与私有IP 10.74.4.11关联的公共IP。
curl -i --interface 10.74.4.11 "ifconfig.co"
我只想将此linux命令转换为可以在程序中使用的Java HTTP请求。
I have written a code to get all the private ips associated with each interface. I'm storing all the private ips in ArrayList ips
.
List<String> ips = new ArrayList<>();
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
ips.add(enumIpAddr.nextElement().toString().replace("/", ""));
}
}
Now I want to iterate over the List ips and want to call the http request to get the public ip. Below is the linux curl command that will return public ip associated with the private ip 10.74.4.11.
curl -i --interface 10.74.4.11 "ifconfig.co"
I just want convert this linux command to java http request which I can use in my program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此方法使用Java Runtime API执行命令,然后将发现的公共IP添加到Hashmap中,作为内部Internip密钥的值。因此,最后,您可以通过搜索hashmap“ ips.get(“ internalip”)”来找到所需的公共IP。
This method uses Java Runtime API to execute the command and then adds the found public ip to the hashmap as a value to the internalIp key. So finally you can find the desired public ip by searching through the hashmap "ips.get("internalIp")"