java.net.URL例子

    /**
     * 网页内容获取
     * @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();
        }
    }

你可能感兴趣的:(java.net.URL例子)