编程要求根据提示使用URL类和InetAddress类编程在右侧编辑器 Begin-End 补充代码。使用javanet的URL类获取URL的各个部分参数使用InetAddress类的getAllByName方法输出百度主机的所有地址测试说明平台会对你编写的代码进行测试:测试输入:预期输出:协议为:http主机名:wwwrunoobcom路径:indexhtml端口:-1请求参数:language
package step1;
import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException;
public class Step1Test {
public static void main(String[] args) {
// URL地址:http://www.runoob.com/index.html?language=cn#javase
// 1. 要求输出:协议、主机名、路径、端口、请求参数
// ********** Begin **********
try {
URL url = new URL("http://www.runoob.com/index.html?language=cn#javase");
System.out.println("协议为:" + url.getProtocol());
System.out.println("主机名:" + url.getHost());
System.out.println("路径:" + url.getPath());
System.out.println("端口:" + url.getPort());
System.out.println("请求参数:" + url.getQuery());
} catch (MalformedURLException e) {
e.printStackTrace();
}
// 2.输出百度的所有主机地址
try {
InetAddress[] addresses = InetAddress.getAllByName("www.baidu.com");
for (InetAddress address : addresses) {
System.out.println(address.getHostName() + "/" + address.getHostAddress());
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
// ********** End **********
}

原文地址: http://www.cveoy.top/t/topic/fi3U 著作权归作者所有。请勿转载和采集!