Skip to content

Instantly share code, notes, and snippets.

@Carlos-Augusto
Last active September 6, 2023 09:46
Show Gist options
  • Save Carlos-Augusto/92ed5ad616da212ef7199f955f413567 to your computer and use it in GitHub Desktop.
Save Carlos-Augusto/92ed5ad616da212ef7199f955f413567 to your computer and use it in GitHub Desktop.
package com.flatmappable.models;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.util.Arrays;
public class ClientCustomSSL {
public static void main(String[] args) throws Exception {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("/path_to_keystore/keystore"), "changeme".toCharArray());
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(ks, "changeme".toCharArray()).build();
try (CloseableHttpClient httpclient = HttpClients.custom()
.setSSLContext(sslcontext)
.build()) {
HttpGet httpget = new HttpGet("https://api.certify.demo.ubirch.com");
try (CloseableHttpResponse response = httpclient.execute(httpget)) {
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(entity));
}
HttpPost httpPost = new HttpPost("https://api.certify.demo.ubirch.com/api/certify/v2/issue");
String s = """
{
"nam":{
"fn":"MüllèČÇ",
"gn":"Eriká"
},
"dob":"1964-08-12",
"t":[
{
"id":"8888888",
"tg":"840539006",
"tt":"LP6464-4",
"nm":"Roche LightCycler qPCR",
"tr":"260415000",
"sc":"2021-04-13T14:20:00+00:00",
"dr":"2021-04-13T20:00:01+00:00",
"tc":"Hauptbahnhof Köln"
}
]
}""";
httpPost.setEntity(new StringEntity(s, ContentType.create("application/vnd.dgc.v1.3+json", StandardCharsets.UTF_8)));
httpPost.setHeader("Accept", "application/cbor+base45");
try (CloseableHttpResponse response1 = httpclient.execute(httpPost)) {
HttpEntity entity = response1.getEntity();
System.out.println("----------------------------------------");
System.out.println(response1.getStatusLine());
Arrays.stream(response1.getAllHeaders()).forEach(a -> System.out.println(a.getName() + " - " + a.getValue()));
System.out.println(EntityUtils.toString(entity));
}
}
}
}
@Carlos-Augusto
Copy link
Author

Note that in some cases it is recommended to create pkcs12 keystore for both compatibility and security reasons. In that case you can load the keystore like this:

 KeyStore ks = KeyStore.getInstance("pkcs12");
        ks.load(new FileInputStream("/path_to_keystore/keystore"), "changeme".toCharArray());
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(ks, "changeme".toCharArray()).build();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment