2

Our android app is referring to HTTP URL to get some data from server. It was working properly till 2 days ago but suddenly we get "sslpeerunverifiedexception: no peer certificate" exception while no changes happen neither in our code nor in server. The code is quite simple:

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 12000);
HttpConnectionParams.setSoTimeout(httpParameters, 12000);
HttpClient client = new DefaultHttpClient(httpParameters);
HttpGet request = new HttpGet("http://site.com");
HttpResponse httpResponse = client.execute(request);
Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
kazemipouya
  • 95
  • 1
  • 1
  • 8

2 Answers2

1

Add below function in your code.

public HttpClient getNewHttpClient() throws SocketException, UnknownHostException {
    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();
    }
}

And Use below code for call above function.

HttpClient httpclient = getNewHttpClient();
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
0

It sounds like the server you're accessing is using a self-signed SSL certificate.

While not recommended (could be a MiM attack) you can just ignore this. See this post for more info: Self-signed SSL acceptance on Android

Community
  • 1
  • 1
Chris Banes
  • 31,763
  • 16
  • 59
  • 50