服务器返回 HTTP 响应代码:429 for URL JAVA Reddit JSON

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: Server returned HTTP response code: 429 for URL: https://www.reddit.com/r/arabfunny/top.json?limit=100

修复了将内容类型设置为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);
    }
}

问题已通过添加解决:

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");