Java请求Reddit JSON接口返回HTTP 429错误的求助
解决Reddit API返回HTTP 429错误的问题
That 429 error is Reddit's rate limiting mechanism telling you your requests are either coming in too fast, or more likely, your request lacks the required headers to be recognized as a valid client. Reddit's public API requires every request to include a User-Agent header—without it, Reddit flags your requests as suspicious and throttles them immediately.
Here's how to fix your code by adding the mandatory header:
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(); // Add the required User-Agent header // Replace this with your app name and contact info (like your email) request.setRequestProperty("User-Agent", "ArabFunnyPostScraper/1.0 (jane.smith@example.com)"); request.connect(); JsonParser jp = new JsonParser(); JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); JsonObject rootobj = root.getAsJsonObject(); // Add your logic to process the retrieved JSON data here }
Important Reminders:
- The User-Agent should clearly identify your application and include a contact email—this helps Reddit reach out if your requests ever cause unintended issues.
- If you still hit rate limits after adding the header, check the
Retry-Afterresponse header to see how long you need to wait before making another request. - For more robust Reddit API interactions, consider using a dedicated Java library like JRAW (Java Reddit API Wrapper), which handles rate limiting, authentication, and JSON parsing out of the box.
内容的提问来源于stack exchange,提问作者Luke Prior
相关产品推荐
相关产品推荐

