Example usage for com.squareup.okhttp CertificatePinner pin

List of usage examples for com.squareup.okhttp CertificatePinner pin

Introduction

In this page you can find the example usage for com.squareup.okhttp CertificatePinner pin.

Prototype

public static String pin(Certificate certificate) 

Source Link

Document

Returns the SHA-1 of certificate 's public key.

Usage

From source file:co.paralleluniverse.fibers.okhttp.CallTest.java

License:Open Source License

@Test
public void matchingPinnedCertificate() throws Exception {
    server.get().useHttps(sslContext.getSocketFactory(), false);
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());

    client.setSslSocketFactory(sslContext.getSocketFactory());
    client.setHostnameVerifier(new RecordingHostnameVerifier());

    // Make a first request without certificate pinning. Use it to collect certificates to pin.
    Request request1 = new Request.Builder().url(server.getUrl("/")).build();
    Response response1 = FiberOkHttpUtil.executeInFiber(client, request1);
    CertificatePinner.Builder certificatePinnerBuilder = new CertificatePinner.Builder();
    for (Certificate certificate : response1.handshake().peerCertificates()) {
        certificatePinnerBuilder.add(server.get().getHostName(), CertificatePinner.pin(certificate));
    }//from  w  w  w .  ja va  2 s  .c  om

    // Make another request with certificate pinning. It should complete normally.
    client.setCertificatePinner(certificatePinnerBuilder.build());
    Request request2 = new Request.Builder().url(server.getUrl("/")).build();
    Response response2 = FiberOkHttpUtil.executeInFiber(client, request2);
    assertNotSame(response2.handshake(), response1.handshake());
}

From source file:com.anony.okhttp.sample.CertificatePinning.java

License:Apache License

public void run() throws Exception {
    Request request = new Request.Builder().url("https://publicobject.com/robots.txt").build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful())
        throw new IOException("Unexpected code " + response);

    for (Certificate certificate : response.handshake().peerCertificates()) {
        System.out.println(CertificatePinner.pin(certificate));
    }//from ww w  . ja v  a2  s  .  c  om
}