美文网首页
2021-10-09 网络编程(URL-URLConnectio

2021-10-09 网络编程(URL-URLConnectio

作者: Denholm | 来源:发表于2021-11-04 19:45 被阅读0次
clipboard.png
import java.net.URL;

public class URLDemo {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://open.163.com/ocw/");
        System.out.println(url.getProtocol());
        System.out.println(url.getHost());
        System.out.println(url.getFile());
        System.out.println(url.getPath());
        System.out.println(url.getQuery());
    }

}
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class URLConnectionDemo {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://open.163.com/special/opencourse/algorithms.html");
        URLConnection conn = url.openConnection();
//        System.out.println(conn);
        InputStream is = conn.getInputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) != -1) {
            System.out.println(new String(buf, 0, len));
        }
        is.close();
    }

}

相关文章

网友评论

      本文标题:2021-10-09 网络编程(URL-URLConnectio

      本文链接:https://www.haomeiwen.com/subject/hqscoltx.html