Search code examples
javajsonreddit

Server returned HTTP response code: 429 for URL JAVA Reddit JSON


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

Solution

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