J'ai essayé d'appeler l'API Rest de GitHub à partir de l'API Socket de Java
Test.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
public class Test {
//Obtenir des informations sur le référentiel GitHub à l'aide de l'API REST GitHub v3
//Le format de l'API REST v3 est
// /repos/:owner/:repo/contents/:path
//Devient
private static final String GITHUB_RESTAPI_PATH = "https://api.github.com/repos/triple4649/MLLearning_Java/contents/src";
public static void main(String[] args) throws Exception {
HttpsURLConnection con = createHttpsURLConnection(GITHUB_RESTAPI_PATH);
//Informations d'en-tête de sortie
System.out.println("----- Headers -----");
printHeaderFields(con);
//Informations sur le corps de sortie
System.out.println("----- Body -----");
printBody(con);
con.disconnect();
}
//Générer URLConnection pour la connexion TLS(Version non vérifiée de la certification)
private static HttpsURLConnection createHttpsURLConnection (String path) throws Exception{
SSLSocketFactory factory = null;
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new NonAuthentication[] { new NonAuthentication() },
null);
factory = ctx.getSocketFactory();
URL url = new URL(path);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setSSLSocketFactory(factory);
return con;
}
//Informations d'en-tête de sortie
private static void printHeaderFields(HttpsURLConnection con){
con.getHeaderFields()
.entrySet()
.stream()
.map(e->String.format("key:%s value:%s", e.getKey(),e.getValue()))
.forEach(System.out::println);
}
//Informations sur le corps de sortie
private static void printBody(HttpsURLConnection con) throws Exception{
new BufferedReader(new InputStreamReader(
con.getInputStream()))
.lines()
.forEach(System.out::println);
}
}
class NonAuthentication implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
Vous pouvez obtenir des informations sur le référentiel GitHub au format JSON, comme indiqué ci-dessous.
[{"name":"base64",
"path":"src/base64",
"sha":"7fe220c6549e5c90c49a992c8524688122609944",
"size":0,
"url":"https://api.github.com/repos/triple4649/MLLearning_Java/contents/src/base64?ref=master",
"html_url":"https://github.com/triple4649/MLLearning_Java/tree/master/src/base64",
"git_url":"https://api.github.com/repos/triple4649/MLLearning_Java/git/trees/7fe220c6549e5c90c49a992c8524688122609944",
"download_url":null,
"type":"dir",
"_links":{"self":"https://api.github.com/repos/triple4649/MLLearning_Java/contents/src/base64?ref=master",
"git":"https://api.github.com/repos/triple4649/MLLearning_Java/git/trees/7fe220c6549e5c90c49a992c8524688122609944",
"html":"https://github.com/triple4649/MLLearning_Java/tree/master/src/base64"}},
Recommended Posts