给我写一段不需要token获取github开源项目所有分支的java代码
时间: 2024-05-14 10:13:03 浏览: 8
好的,以下是一段获取Github开源项目所有分支的Java代码,无需Token:
```java
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class GithubBranches {
public static void main(String[] args) throws IOException {
String owner = "ownerName"; // Github账户名
String repo = "repoName"; // Github项目名
URL url = new URL("https://api.github.com/repos/" + owner + "/" + repo + "/branches");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0"); // Github默认只接受请求头中包含User-Agent的请求
conn.setRequestMethod("GET");
Scanner sc = new Scanner(conn.getInputStream());
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
}
}
```
希望这个代码对你有帮助!请问您有没有其它问题需要我回答呢?