从URL请求中获取返回的html

时间:2013-09-05 11:50:12

标签: java html request

我有这段代码:

try {
        URL url = new URL("http://www.gooel.com");

        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
        httpCon.setDoOutput(true);
        //httpCon.setRequestMethod("GET");
        OutputStreamWriter out = new OutputStreamWriter(
            httpCon.getOutputStream());
        out.write("Resource content");
        out.close();
        // httpCon.getInputStream();

        BufferedReader in = new BufferedReader(new InputStreamReader(
      httpCon.getInputStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

如何从此连接获取返回的html并将其打印在屏幕上? 这不起作用。

谢谢,

1 个答案:

答案 0 :(得分:0)

最简单的答案: google HttpURLConnection

使用第一击: Java: Simple HTTPUrlConnection example

你完成了!