title: SSLHandshakeException with Retrofit on Android
body: <p>I know that there were already similiar questions, but none of them gave me solution for this problem. </p>
  
  <p>I am trying to connect to some XML API, if I do this in Postman or just by browser everything works fine and XML is being returned.</p>
  
  <p>However, if I send <code>GET</code> to this URL I get : </p>
  
  <pre><code>W/System.err: javax.net.ssl.SSLHandshakeException: Connection closed by peer
  W/System.err:     at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
  W/System.err:     at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318)
  W/System.err:     at com.squareup.okhttp.Connection.connectTls(Connection.java:239)
  W/System.err:     at com.squareup.okhttp.Connection.connectSocket(Connection.java:201)
  W/System.err:     at com.squareup.okhttp.Connection.connect(Connection.java:172)
  W/System.err:     at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:358)
  W/System.err:     at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:117)
  W/System.err:     at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
  W/System.err:     at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
  W/System.err:     at com.squareup.okhttp.Call.getResponse(Call.java:276)
  W/System.err:     at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:234)
  W/System.err:     at com.squareup.okhttp.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:136)
  W/System.err:     at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:223)
  W/System.err:     at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:196)
  W/System.err:     at com.squareup.okhttp.Call.access$100(Call.java:34)
  W/System.err:     at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:162)
  W/System.err:     at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
  W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  W/System.err:     at java.lang.Thread.run(Thread.java:818)
  W/System.err:   Suppressed: javax.net.ssl.SSLHandshakeException: Connection closed by peer
  W/System.err:       ... 20 more
  </code></pre>
  
  <p>My first question - why it works in browser but not in Android?
  My second question - how to solve this? Is it problem with Android or server? I have tried these two solutions, but none of them works : </p>
  
  <pre><code>    private static OkHttpClient createClient() {
      OkHttpClient client = new OkHttpClient();
      HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
      client.interceptors().add(interceptor);
      ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
              .tlsVersions(TlsVersion.TLS_1_2)
              .cipherSuites(
                      CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                      CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                      CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
                      CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
                      CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
                      CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
                      CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
                      CipherSuite.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
                      CipherSuite.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
                      CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
                      CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
                      CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA)
              .build();
      client.setConnectionSpecs(Collections.singletonList(spec));
      return client;
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code> private static OkHttpClient createClient() {
      OkHttpClient client = new OkHttpClient();
      HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
      client.interceptors().add(interceptor);
      try {
          TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
              @Override
              public void checkClientTrusted(
                      java.security.cert.X509Certificate[] chain,
                      String authType) throws CertificateException {
              }
  
              @Override
              public void checkServerTrusted(
                      java.security.cert.X509Certificate[] chain,
                      String authType) throws CertificateException {
              }
  
              @Override
              public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                  return null;
              }
          }};
  
          SSLContext sslContext = SSLContext.getInstance("SSL");
          sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
          SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
          client.setSslSocketFactory(sslSocketFactory);
      } catch (NoSuchAlgorithmException | KeyManagementException e) {
          e.printStackTrace();
      }
      return client;
  }
  </code></pre>