Skip to content

Instantly share code, notes, and snippets.

Created September 6, 2023 02:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
POST a JSON to a URL with insecure or invalid SSL certs using latest JDK 11 HttpClient
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public[] getAcceptedIssuers() {
return null;
public void checkClientTrusted([] certs, String authType) {
public void checkServerTrusted([] certs, String authType) {
try {
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new;
// Creat HttpClient with new SSLContext.
HttpClient httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofMillis(3 * 1000))
.sslContext(sc) // SSL context 'sc' initialised as earlier
// Create request.
HttpRequest request = HttpRequest.newBuilder()
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(<PAYLOAD JSON STRING>))
//Send Request
HttpResponse<String> response =
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
//Access response JSON
String responseJson = response.body();
} catch (Exception e) {
throw new Exception(e);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment