如何查看计算机端口号,Java怎么判断URL是域名格式还是IP加端口格式?
答案:1 悬赏:10 手机版
解决时间 2021-05-11 07:39
- 提问者网友:喧嚣尘世
- 2021-05-11 02:10
如何查看计算机端口号,Java怎么判断URL是域名格式还是IP加端口格式?
最佳答案
- 五星知识达人网友:老鼠爱大米
- 2021-05-11 03:24
简单的办法是判断host格式,复杂的但稳妥的办法是当做域名处理,域名解析,如果解析出来的IP和域名字符串相等,那就是说当做的域名,其实是ip。
try {
URL url=new URL(www.sina.com.cn);
String host=url.getHost();
InetAddress address = null;
address = InetAddress.getByName(host);
if(host.equalsIgnoreCase(address.getHostAddress()))
System.out.println(ip);
else
System.out.println(domain);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
URL url=new URL(www.sina.com.cn);
String host=url.getHost();
InetAddress address = null;
address = InetAddress.getByName(host);
if(host.equalsIgnoreCase(address.getHostAddress()))
System.out.println(ip);
else
System.out.println(domain);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯