Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diyan/767897fa7b14850a14c6f9a2bd909952 to your computer and use it in GitHub Desktop.
Save diyan/767897fa7b14850a14c6f9a2bd909952 to your computer and use it in GitHub Desktop.
Configure Apache HttpComponents backend for Google HTTP Client
package com.example.project;

import java.io.IOException;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.protocol.HttpContext;

// 1. Ignore SSL certificate errors.
// 2. Set HTTP version 1.1, required by Google HTTP Client, default in Apache HttpComponents v4.3.
// 3. Disable HTTP redirects, required by Google HTTP Client.
// 4. Disable request retries, required by Google HTTP Client.
HttpClient apacheHttpClient = HttpClients.custom()
    .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
    .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
    .disableRedirectHandling()
    .disableAutomaticRetries()
    .build();

HttpTransport httpTransport = new ApacheHttpTransport(apacheHttpClient);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment