gpt4 book ai didi

java - 读取 HTML 时的 Http 响应代码 429

转载 作者:行者123 更新时间:2023-12-02 01:54:12 25 4
gpt4 key购买 nike

在java中,我想从URL(instagram)读取并保存所有HTML,但收到错误429(请求过多)。我认为这是因为我试图读取比请求限制更多的行。

StringBuilder contentBuilder = new StringBuilder();
try {
URL url = new URL("https://www.instagram.com/username");
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String str;
while ((str = in.readLine()) != null) {
contentBuilder.append(str);
}
in.close();
} catch (IOException e) {
log.warn("Could not connect", e);
}
String html = contentBuilder.toString();

错误是这样的;

Could not connect
java.io.IOException: Server returned HTTP response code: 429 for URL: https://www.instagram.com/username/

它还表明由于这一行而发生错误

InputStream is =con.getInputStream();

有谁知道我为什么会收到此错误和/或如何解决它?

最佳答案

该问题可能是由于连接未关闭/断开造成的。对于用于自动关闭的输入 try-with-resources,即使在异常或返回时也很有用。此外,您还构建了一个 InputStreamReader,它将使用运行应用程序的计算机的默认编码,但您需要 URL 内容的字符集。readLine 返回没有行结尾的行(这通常非常有用)。所以添加一个。

StringBuilder contentBuilder = new StringBuilder();
try {
URL url = new URL("https://www.instagram.com/username");
URLConnection con = url.openConnection();
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream(), "UTF-8"))) {
String line;
while ((line = in.readLine()) != null) {
contentBuilder.append(line).append("\r\n");
}
} finally {
con.disconnect();
} // Closes in.
} catch (IOException e) {
log.warn("Could not connect", e);
}
String html = contentBuilder.toString();

关于java - 读取 HTML 时的 Http 响应代码 429,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52551174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com