【发布时间】:2020-11-28 23:04:09
【问题描述】:
我正在尝试获取此处找到的 JSON 文件:https://www.reddit.com/r/arabfunny/top.json?limit=100
我有以下代码:
static void getPost() throws Exception {
String webPage = "https://www.reddit.com/r/arabfunny/top.json?limit=100";
URL url = new URL(webPage);
URLConnection request = url.openConnection();
request.connect();
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
JsonObject rootobj = root.getAsJsonObject();
}
这段代码在运行时抛出如下错误:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 429 for URL: https://www.reddit.com/r/arabfunny/top.json?limit=100
【问题讨论】:
-
HTTP 状态码 429:用户在给定时间内发送了太多请求(“速率限制”)。
-
为什么我的代码发送了多个请求?我只希望它下载一次文件。我应该看/改变什么?
-
你在短时间内测试了你的代码多少次?每个测试计为 100 个请求。
-
使用 Spring Boot 你可以使用RestTemplate,你可以看到一个快速指南here。这将帮助您非常轻松地通过http方法使用Api rest。
-
这是第一次,我怎样才能让它只做一个请求?