title: Spring RestTemplate I/O error: No peer certificate
body: <p>I always get the same error whens try to get a https resource:</p>
  
  <pre><code>org.springframework.web.client.ResourceAccessException: I/O error: No peer certificate; nested exception is javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
  </code></pre>
  
  <p>I have a self-signed virtual host where my app runs, the app works fine on <code>http</code> but I need <code>https</code>.</p>
  
  <p>Here is my code in android app:</p>
  
  <pre><code>mRestTemplate = new RestTemplate();
  mRestTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
  mRestTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
  
  final ResponseObject responseObject = mRestTemplate.postForObject(APP_URL, requestObject, ResponseObject.class);
  </code></pre>
  
  <hr>
  
  <h2>Update 1</h2>
  
  <ul>
  <li><p>I tried the solution proposed by @nilesh and has not worked.</p></li>
  <li><p>I tried <a href="https://stackoverflow.com/a/17159839/816721">this solution</a> with the same error</p>
  
  <pre><code>HttpParams params = new BasicHttpParams();
  HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
  HttpProtocolParams.setUseExpectContinue(params, true);
  
  SchemeRegistry schReg = new SchemeRegistry();
  schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
  schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
  ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
  
  client = DefaultHttpClient(conMgr, params);
  
  final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
      factory.setHttpClient(client);
  
  mRestTemplate = new RestTemplate();
  mRestTemplate.setRequestFactory(factory);
  </code></pre></li>
  <li><p>I tried <a href="http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/" rel="nofollow noreferrer">this solution</a> without success and the same error</p>
  
  <ol>
  <li>Grab all required certificates (root and any intermediate CA’s)</li>
  <li>Create a keystore with keytool and the BouncyCastle provider and import the certs</li>
  <li>Load the keystore in your android app and use it for the secured connections
  Don’t use the standard java.net.ssl.HttpsURLConnection for the secure connection. Use the Apache HttpClient (Version 4 atm) library, which is already built-in in android. It’s built on top of the java connection libraries and is, in my opinion, faster, better modularized and easier to understand.</li>
  </ol></li>
  </ul>