Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我能';t使用Java代码接收网站的所有HTML内容_Java_Html - Fatal编程技术网

我能';t使用Java代码接收网站的所有HTML内容

我能';t使用Java代码接收网站的所有HTML内容,java,html,Java,Html,我使用Java来接收网站的HTML内容。但我无法接收它的全部内容。有时,我会收到以“…”结尾的内容,而不是内容的结尾。你能告诉我什么是错误吗。下面是我的代码: public static String requestHtmlContent(String sUrl){ URL url; URLConnection connection; StringBuilder strBuilder = new StringBuilder(); B

我使用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();
    }

在不实际测试代码的情况下,我可以这样说,
,而
循环肯定是可疑的:

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

while条件消耗了来自读卡器的一行代码,这行代码永远不会被处理。

如果不实际测试代码,我可以说while循环肯定是可疑的:

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

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

StackOverflow不是这个问题的合适位置。我们不进行代码调试。您需要自己进行调试,如果您不确定某些东西为什么不能按预期工作,请在发布代码时解释您希望它做什么,以及实际正在做什么,包括所有错误消息。StackOverflow不是解决此问题的合适位置。我们不进行代码调试。您需要自己进行调试,如果您不确定为什么某些东西不能按预期工作,请在发布代码时解释您希望它执行的操作,以及实际执行的操作,包括所有错误消息。+1很好的捕获。另外,由于
StringBuilder
主要用于避免字符串连接
strBuilder.append(line+“\n”)
应该是
strBuilder.append(line).append('\n')
@bowmoer:但我以前有过getInputStream()。或者你能给我一个解决方案。谢谢谢谢你,Pshemo.just remove
line=reader.readLine()
while
中读取,因此条件中读取的行将得到处理。@bowmore:谢谢。我刚找到问题的原因。那个代码是要删除“;”中末尾的“while”行。但问题的主要原因是,“…”在结果的末尾,因为我在调试工具上看到了字符串,而调试只显示了一个限制数字字符。+1捕捉得很好。另外,由于
StringBuilder
主要用于避免字符串连接
strBuilder.append(line+“\n”)
应该是
strBuilder.append(line).append('\n')
@bowmoer:但我以前有过getInputStream()。或者你能给我一个解决方案。谢谢谢谢你,Pshemo.just remove
line=reader.readLine()
while
中读取,因此条件中读取的行将得到处理。@bowmore:谢谢。我刚找到问题的原因。那个代码是要删除“;”中末尾的“while”行。但问题的主要原因是,“…”在结果的末尾,因为我在调试工具上看到字符串,而调试只显示一个限制数字字符。