T
T
themir2014-02-28 12:51:05
Java
themir, 2014-02-28 12:51:05

HTTPS request in Android?

I'm trying to send a POST request via SSL to Android. The code is like this:

KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
        
SSLSocketFactory sf = new SSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        
httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParams, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(httpParams, true);
        
SchemeRegistry sr = new SchemeRegistry();
sr.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
sr.register(new Scheme("https", sf, 443));
ccManager = new ThreadSafeClientConnManager(httpParams, sr);

HttpClient client = = new DefaultHttpClient(ccManager, httpParams);  	
HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse responce = client.execute(post);       		  
in = new BufferedReader(new InputStreamReader(responce.getEntity().getContent()));    		
String line = "";           	
while ((line = in.readLine()) != null) sb.append(line);     	
in.close();

As a result, we get an exception with the message: No peer certificate . Tell me, please, what is the problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
themir, 2014-02-28
@themir

Answer to my question:

KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
                
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                
httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParams, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(httpParams, true);
                
SchemeRegistry sr = new SchemeRegistry();
sr.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
sr.register(new Scheme("https", sf, 443));
ccManager = new ThreadSafeClientConnManager(httpParams, sr);

HttpClient client = = new DefaultHttpClient(ccManager, httpParams);  	
HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse responce = client.execute(post);       		  
in = new BufferedReader(new InputStreamReader(responce.getEntity().getContent()));    		
String line = "";           	
while ((line = in.readLine()) != null) sb.append(line);     	
in.close();

MySSLSocketFactory class:
public class MySSLSocketFactory extends SSLSocketFactory {
  private 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();
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question