服务器返回HTTP响应代码:429,用于URL JAVA Reddit JSON。

huangapple go评论77阅读模式
英文:

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

服务器返回HTTP响应代码:429,用于URL JAVA Reddit JSON。


<details>
<summary>英文:</summary>

Fix problem is set **Content type** to `UrlConnection`

    request.setRequestProperty(&quot;Content-Type&quot;, &quot;application/json; utf-8&quot;);

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 = &quot;https://www.reddit.com/r/arabfunny/top.json?limit=100&quot;;
    
            URL url = new URL(webPage);
            URLConnection request = url.openConnection();
            request.setRequestProperty(&quot;Content-Type&quot;, &quot;application/json; utf-8&quot;);
    
            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(&quot;User-Agent&quot;, &quot;Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2&quot;);

</details>



huangapple
  • 本文由 发表于 2020年8月9日 09:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63321616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定