An HTTP & SPDY client for Android and Java applications with efficient connection pooling, interceptors, and modern protocol support
—
Authentication handling, certificate pinning, TLS handshake management, and credential utilities.
Responds to HTTP authentication challenges (401/407 responses).
public interface Authenticator {
Request authenticate(Proxy proxy, Response response) throws IOException;
Request authenticateProxy(Proxy proxy, Response response) throws IOException;
}Constrains trusted certificates by pinning specific certificate hashes.
public final class CertificatePinner {
public static final CertificatePinner DEFAULT;
public static String pin(Certificate certificate);
public void check(String hostname, List<Certificate> peerCertificates) throws SSLPeerUnverifiedException;
public void check(String hostname, Certificate... peerCertificates) throws SSLPeerUnverifiedException;
}Factory for HTTP authorization credentials.
public final class Credentials {
public static String basic(String userName, String password);
}RFC 2617 authentication challenge from HTTP response.
public final class Challenge {
public Challenge(String scheme, String realm);
public String getScheme();
public String getRealm();
}Record of TLS handshake with certificate chain information.
public final class Handshake {
public static Handshake get(SSLSession session);
public static Handshake get(String cipherSuite, List<Certificate> peerCertificates, List<Certificate> localCertificates);
public String cipherSuite();
public List<Certificate> peerCertificates();
public Principal peerPrincipal();
public List<Certificate> localCertificates();
public Principal localPrincipal();
}Usage Examples:
// Basic authentication
client.setAuthenticator(new Authenticator() {
@Override
public Request authenticate(Proxy proxy, Response response) {
String credential = Credentials.basic("username", "password");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
@Override
public Request authenticateProxy(Proxy proxy, Response response) {
return null; // No proxy auth
}
});
// Certificate pinning
CertificatePinner pinner = new CertificatePinner.Builder()
.add("api.example.com", "sha1/ABC123...")
.build();
client.setCertificatePinner(pinner);Install with Tessl CLI
npx tessl i tessl/maven-com-squareup-okhttp--okhttp