使用URL字符串获取IP地址? (JAVA) [英] Get IP address with URL string? (Java)

查看:140
本文介绍了使用URL字符串获取IP地址? (JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,用户输入一个url字符串,比如说

In my program a user enters a url string, say

http://www.engineering.uiowa.edu/~hawkeng//fall01/graphics/potato.gif

我如何获得此网址的IP地址?我尝试使用

how would I get the IP address of this url? I tried using

InetAddress address = InetAddress.getByName(urlStr);

但结果总是返回null。获取IP地址的正确方法是什么?

but the result always comes back null. What is the proper way to get the IP address?

推荐答案

试试这个:

InetAddress address = InetAddress.getByName(new URL(urlString).getHost());

要获取的原始IP:

String ip = address.getHostAddress();

这篇关于使用URL字符串获取IP地址? (JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆