10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HttpClientにて自己証明書や、ルート証明書なしでのSSLアクセスを可能とする

Posted at

単純に証明書のチェックを無効化させる

DefaultHttpClient
/*
 * 証明書のチェック処理を無効化する
 */
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[]{new X509TrustManager() {
        
    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
    
    @Override
    public void checkClientTrusted(X509Certificate[] certs, String authType) {
        // 何もしない
    }
    
    @Override
    public void checkServerTrusted(X509Certificate[] certs, String authType) {
        // 何もしない
    }
}}, new SecureRandom());

Scheme sch = new Scheme("https", new SSLSocketFactory(sslContext), 443);

DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getConnectionManager().getSchemeRegistry().register(sch);
HttpsURLConnection
SSLContext sslContext = SSLContext.getInstance("SSL");
TrustManager tm = new X509TrustManager() {

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }

    @Override
    public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
    }

    @Override
    public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
    }
};
sslContext.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory socketFactory = sslContext.getSocketFactory();

HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory);
Client client = Client.create();
10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?