JAVA网络编程URLConnection类

import org.apache.commons.io.IOUtils;

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://www.baidu.com"); //制定操作的URL
        URLConnection urlConnection = url.openConnection(); //建立连接
        System.out.println(IOUtils.toString((InputStream) urlConnection.getContent(), "utf-8")); //取得内容
        System.out.println(urlConnection.getContentLength()); //取得内容大小
        System.out.println(urlConnection.getContentType()); //取得内容类型
    }
}
posted @ 2020-12-11 21:01  xl4ng  阅读(123)  评论(0编辑  收藏  举报