Skip to content

Instantly share code, notes, and snippets.

@handstandsam
Created April 7, 2017 17:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save handstandsam/d7d7956a05698b23cad14dd404350e23 to your computer and use it in GitHub Desktop.
Save handstandsam/d7d7956a05698b23cad14dd404350e23 to your computer and use it in GitHub Desktop.
OkHttp 3 SSL Handshake Interceptor - Prints TLS Version & Cipher Suite Used
import android.util.Log;
import java.io.IOException;
import okhttp3.CipherSuite;
import okhttp3.Handshake;
import okhttp3.Response;
import okhttp3.TlsVersion;
/** Prints TLS Version and Cipher Suite for SSL Calls through OkHttp3 */
public class SSLHandshakeInterceptor implements okhttp3.Interceptor {
private static final String TAG = "OkHttp3-SSLHandshake";
@Override
public Response intercept(Chain chain) throws IOException {
final Response response = chain.proceed(chain.request());
printTlsAndCipherSuiteInfo(response);
return response;
}
private void printTlsAndCipherSuiteInfo(Response response) {
if (response != null) {
Handshake handshake = response.handshake();
if (handshake != null) {
final CipherSuite cipherSuite = handshake.cipherSuite();
final TlsVersion tlsVersion = handshake.tlsVersion();
Log.v(TAG, "TLS: " + tlsVersion + ", CipherSuite: " + cipherSuite);
}
}
}
}
@RayyanCoIT
Copy link

can you please guide, how to call this class and execute the method

@yesmar12
Copy link

yesmar12 commented Oct 19, 2020

How I was able to apply this intercepter in my program was to use code like this:

OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(new SSLHandshakeInterceptor())
    .build();

For more details read about interceptors with okhttp

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