通过URL获取HTML

时间:2013-08-24 16:39:53

标签: java string url null stringbuilder

我试图通过URL获取一些HTML并将其放入String中。这是我的努力:

public class 

    Bank {
        public static void main(String[] args) throws IOException {
            URL hh = new URL("https://m.hh.ru/");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(hh.openStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null) {

                inputLine.concat(inputLine);//returns null. WTF?
                System.out.println(inputLine);

            }
            in.close();
            System.out.println(inputLine);
        }
        }

正如我所说,我想把它放到inputLine,但它会返回NULL。 怎么了?

1 个答案:

答案 0 :(得分:3)

使用StringBuilder进行字符串连接。循环应如下所示:

String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
    sb.append(inputLine).append("\n");
}
System.out.println(sb);