title: javax.net.ssl.SSLException: SSL handshake aborted Connection reset by peer while calling webservice Android
body: <p>I am calling <strong>https webservice</strong> and its works fine before, but now when i am trying to call it its give me following errors.</p>
  
  <p><strong>Log Errors :</strong></p>
  
  <pre><code>12-23 06:28:11.969: W/System.err(3014): javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x1cc160: I/O error during system call, Connection reset by peer
  12-23 06:28:11.979: W/System.err(3014):     at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
  12-23 06:28:11.979: W/System.err(3014):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:474)
  12-23 06:28:11.979: W/System.err(3014):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.&lt;init&gt;(OpenSSLSocketImpl.java:750)
  12-23 06:28:11.979: W/System.err(3014):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:692)
  12-23 06:28:11.979: W/System.err(3014):     at crittercism.android.aa.getInputStream(Unknown Source)
  12-23 06:28:11.979: W/System.err(3014):     at org.apache.http.impl.io.SocketInputBuffer.&lt;init&gt;(SocketInputBuffer.java:93)
  12-23 06:28:11.979: W/System.err(3014):     at org.apache.http.impl.SocketHttpClientConnection.createSessionInputBuffer(SocketHttpClientConnection.java:83)
  12-23 06:28:11.979: W/System.err(3014):     at org.apache.http.impl.conn.DefaultClientConnection.createSessionInputBuffer(DefaultClientConnection.java:170)
  12-23 06:28:11.979: W/System.err(3014):     at org.apache.http.impl.SocketHttpClientConnection.bind(SocketHttpClientConnection.java:106)
  12-23 06:28:11.979: W/System.err(3014):     at org.apache.http.impl.conn.DefaultClientConnection.openCompleted(DefaultClientConnection.java:129)
  12-23 06:28:11.979: W/System.err(3014):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:171)
  12-23 06:28:11.989: W/System.err(3014):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
  12-23 06:28:11.989: W/System.err(3014):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
  12-23 06:28:11.989: W/System.err(3014):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
  12-23 06:28:11.989: W/System.err(3014):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
  12-23 06:28:11.989: W/System.err(3014):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
  12-23 06:28:11.989: W/System.err(3014):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
  </code></pre>
  
  <p>I am using following code for calling https webservice.</p>
  
  <pre><code>public static void trustAllHosts() {
  
          X509TrustManager easyTrustManager = new X509TrustManager() {
  
              public void checkClientTrusted(X509Certificate[] chain,
                      String authType) throws CertificateException {
                  // Oh, I am easy!
              }
  
              public void checkServerTrusted(X509Certificate[] chain,
                      String authType) throws CertificateException {
                  // Oh, I am easy!
              }
  
              public X509Certificate[] getAcceptedIssuers() {
                  return null;
              }
  
          };
  
          // Create a trust manager that does not validate certificate chains
          TrustManager[] trustAllCerts = new TrustManager[] { easyTrustManager };
  
          // Install the all-trusting trust manager
          try {
              SSLContext sc = SSLContext.getInstance("TLS");
  
              sc.init(null, trustAllCerts, new java.security.SecureRandom());
  
              HttpsURLConnection
                      .setDefaultSSLSocketFactory(sc.getSocketFactory());
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public static HttpClient getNewHttpClient() {
          try {
              KeyStore trustStore = KeyStore.getInstance(KeyStore
                      .getDefaultType());
              trustStore.load(null, null);
  
              SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
              sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  
              HttpParams params = new BasicHttpParams();
              HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
              HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
  
              SchemeRegistry registry = new SchemeRegistry();
              registry.register(new Scheme("http", PlainSocketFactory
                      .getSocketFactory(), 80));
              registry.register(new Scheme("https", sf, 443));
  
              ClientConnectionManager ccm = new ThreadSafeClientConnManager(
                      params, registry);
  
              return new DefaultHttpClient(ccm, params);
          } catch (Exception e) {
              return new DefaultHttpClient();
          }
      }
  </code></pre>
  
  <p><strong>MySSLSocketFactory.java</strong></p>
  
  <pre><code>public class MySSLSocketFactory extends SSLSocketFactory {
      SSLContext sslContext = SSLContext.getInstance("TLS");
  
      public MySSLSocketFactory(KeyStore truststore)
              throws NoSuchAlgorithmException, KeyManagementException,
              KeyStoreException, UnrecoverableKeyException {
          super(truststore);
  
          TrustManager tm = new X509TrustManager() {
              public void checkClientTrusted(X509Certificate[] chain,
                      String authType) throws CertificateException {
              }
  
              public void checkServerTrusted(X509Certificate[] chain,
                      String authType) throws CertificateException {
              }
  
              public X509Certificate[] getAcceptedIssuers() {
                  return null;
              }
          };
  
          sslContext.init(null, new TrustManager[] { tm }, null);
      }
  
      @Override
      public Socket createSocket(Socket socket, String host, int port,
              boolean autoClose) throws IOException, UnknownHostException {
          return sslContext.getSocketFactory().createSocket(socket, host, port,
                  autoClose);
      }
  
      @Override
      public Socket createSocket() throws IOException {
          return sslContext.getSocketFactory().createSocket();
      }
  }
  </code></pre>
  
  <p>It is working fine before, but now it fails. There are no any changes made in server. </p>
  
  <p>I already refered</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/a/11478987/1405983">Android-Query - Random SSLExceptions</a>  </li>
  <li><a href="https://stackoverflow.com/questions/11021551/intermittent-connection-reset-by-peer-errors-in-android-connecting-to-net-rest">Intermittent Connection Reset by Peer errors in Android connecting to .NET REST endpoint</a></li>
  <li><a href="https://stackoverflow.com/a/8502679/1405983">Android HTTPS exception Connection reset by peer</a></li>
  <li><a href="https://stackoverflow.com/a/12885248/1405983">Why is HttpUrlConnection throwing an SSLException while on a mobile data connection?</a></li>
  </ol>
  
  <p>I have tested it in both wifi and mobile data. Application not works in both.</p>
  
  <p>If anybody face this problem before then please help me to solve it.</p>