java.net.URL例子

来源:互联网 发布:淘宝直播右下角的心 编辑:程序博客网 时间:2024/05/29 03:15

    /**
     * 网页内容获取
     * @throws IOException
     */
    public void getWebContext() throws IOException {
        InputStream in;
        URL url = new URL("http://localhost:8080/");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection = (HttpURLConnection) url.openConnection();

        // 模拟成IE
        connection.setRequestProperty("User-Agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
//        connection.connect();
        in = connection.getInputStream();
        BufferedReader breader = new BufferedReader(new InputStreamReader(in,
                "GBK"));
        String str = breader.readLine();
        while (str != null) {
            System.out.println(str);
            str = breader.readLine();
        }
    }

原创粉丝点击