java httpurlconnection切断html

时间:2010-07-19 21:03:17

标签: java httpurlconnection

嘿,我正试图从推特个人资料页面获取html,但是httpurlconnection只返回了一小段html。我的代码

for(int i = 0; i < urls.size(); i++)
{
URL url = new URL(urls.get(i));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6");
System.out.println(connection.getResponseCode());
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null)
{
    builder.append(line);
}
String html = builder.toString();
}

我总是得到200作为每次通话的响应代码。然而,大约1/3的时间返回整个html文档,而另一半只返回前几百行。 html被截止时返回的数量并不总是相同。

有什么想法吗?谢谢你的帮助!

其他信息:查看标题后,我似乎得到了重复的内容长度标题。第一个是全长,另一个是更短(并且可能代表我在某些时候得到的长度)如何处理重复的标题?

2 个答案:

答案 0 :(得分:1)

这对我来说很好,我在builder.append(line);之后添加了一个换行符,使其在控制台中更具可读性,但除此之外它还返回了此页面的所有HTML:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class RetrieveHTML {

    public static void main(String[] args) throws IOException {
        List<String> urls = new ArrayList<String>();
        urls.add("http://stackoverflow.com/questions/3285077/java-httpurlconnection-cutting-off-html");

        for (int i = 0; i < urls.size(); i++) {
            URL url = new URL(urls.get(i));
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6");
            System.out.println(connection.getResponseCode());
            String line;
            StringBuilder builder = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append("\n"); 
            }
            String html = builder.toString();
            System.out.println("HTML " + html);
        }

    }
}

答案 1 :(得分:0)