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 **********
}
Java 网络编程:URL 和 InetAddress 类使用示例

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

免费AI点我,无需注册和登录