0

我使用 Java 接收网站的 HTML 内容。但我无法收到它的所有内容。有时,我收到以“...”结尾的内容,而不是内容的结尾。你能告诉我什么是错误。下面是我的代码:

public static String requestHtmlContent(String sUrl){
        URL url;
        URLConnection connection;
        StringBuilder strBuilder = new StringBuilder();
        BufferedReader reader;
        try {
            url = new URL( sUrl );
            connection = url.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1");
            InputStream is = url.openConnection().getInputStream();
            reader = new BufferedReader( new InputStreamReader( is )  );

            String line = null;
            while((line = reader.readLine())!= null);  {
                line = reader.readLine();
                strBuilder.append(line +"\n");
            }
            reader.close();

            return strBuilder.toString();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strBuilder.toString();
    }
4

1 回答 1

3

如果没有实际测试您的代码,我可以说这个while循环绝对是可疑的:

while((line = reader.readLine())!= null);  {
    line = reader.readLine();
    strBuilder.append(line +"\n");
}

while 条件消耗来自阅读器的一行,该行永远不会被处理。

于 2013-01-20T17:36:28.330 回答