使用 Java 获取 Github 个存储库

Use Java to get Github repositories

我正在使用以下代码向 github 发送 http 请求。

String url = "https://api.github.com/repositories";
try {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost(url);
    // StringEntity params = new StringEntity(body);
    request.addHeader("content-type", "application/json");
    // request.setEntity(params);
    HttpResponse result = httpClient.execute(request);
    String json = EntityUtils.toString(result.getEntity(), "UTF-8");

    System.out.println(json);
} catch (IOException ex) {
}

我得到输出:{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}

如果直接在浏览器中输入“https://api.github.com/repositories”,会显示很多有用的信息。我的问题是如何使用 Java 获取使用浏览器时看到的信息。

您应该使用 HttpGet 而不是 HttpPost。就像您的浏览器发送 GET 请求一样。