英文:
Server returned HTTP response code: 429 for URL JAVA Reddit JSON
问题
我正在尝试获取此处找到的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: 服务器返回了HTTP响应代码: 429,URL: https://www.reddit.com/r/arabfunny/top.json?limit=100
英文:
I am trying to get the JSON file found here: https://www.reddit.com/r/arabfunny/top.json?limit=100
I have the following code:
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();
}
This code throws the following error when run:
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
答案1
得分: 1
修复问题是将Content type设置为UrlConnection
request.setRequestProperty("Content-Type", "application/json; utf-8");
完整的代码:
package com.example;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) throws IOException {
String webPage = "https://www.reddit.com/r/arabfunny/top.json?limit=100";
URL url = new URL(webPage);
URLConnection request = url.openConnection();
request.setRequestProperty("Content-Type", "application/json; utf-8");
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
JsonObject rootobj = root.getAsJsonObject();
System.out.println(rootobj);
}
}
<details>
<summary>英文:</summary>
Fix problem is set **Content type** to `UrlConnection`
request.setRequestProperty("Content-Type", "application/json; utf-8");
Full code:
package com.example;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) throws IOException {
String webPage = "https://www.reddit.com/r/arabfunny/top.json?limit=100";
URL url = new URL(webPage);
URLConnection request = url.openConnection();
request.setRequestProperty("Content-Type", "application/json; utf-8");
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
JsonObject rootobj = root.getAsJsonObject();
System.out.println(rootobj);
}
}
[![Output-json][1]][1]
[1]: https://i.stack.imgur.com/hnLEA.jpg
</details>
# 答案2
**得分**: 1
问题已通过添加以下内容得以解决:
request.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
<details>
<summary>英文:</summary>
The problem was fixed by adding:
request.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论