Java怎么判断URL是域名格式还是IP加端口格式?

有没有人在啊,想请问下,Java怎么判断URL是域名格式还是IP加端口格式?
最新回答
痛徹心扉

2024-05-01 09:13:16

简单的办法是判断host格式,复杂的但稳妥的办法是当做域名处理,域名解析,如果解析出来的IP和域名字符串相等,那就是说当做的域名,其实是ip。

try {
URL url=new URL("
http://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();
}
勒非

2024-05-01 11:47:17

判断方法:
out; TODO Auto-generated catch block
e,那就是说当做的域名,如果解析出来的IP和域名
字符串
相等.printStackTrace();
} catch (MalformedURLException e) {
/简单的办法是判断host格式.cn" target="_blank">http;/://www,其实是ip.out.com,复杂的但稳妥的办法是当做域名处理.getByName(host);)://www;
else
System; TODO Auto-generated catch block
e;
InetAddress address = null;/.cn"); if(host.getHostAddress()))
System;domain".println(".printStackTrace();).println(".equalsIgnoreCase(address;
address = InetAddress,
域名解析
;ip".sina.getHost();
} catch (UnknownHostException e) {
/.com.sina。

try {
URL url=new URL("
String host=url
听一首歌流一夜泪

2024-05-01 01:13:58

你看域名解析是什么的。top