2012年7月6日金曜日

Javaでオレオレ証明書のサーバに対してhttpsアクセス

テストでローカルのオレオレ署名のサーバにhttpsアクセスしたいのでウェブでサンプルを探したけど、非推奨パッケージを使っているものが多くて大変でした。

ようやく動いたのがあるのでメモしておく。

以下のソースを参考にしてください。
参照元はここです。
適当なimportを行なって下さい。

private void initHttps(){
  try
  {
   mClient = new DefaultHttpClient();
   final SSLContext sslContext = SSLContext.getInstance("SSL");
   sslContext.init(null, new TrustManager[] { new X509TrustManager() {
    public void checkClientTrusted(X509Certificate[] arg0,
      String arg1) throws CertificateException {
    }
    public void checkServerTrusted(X509Certificate[] arg0,
      String arg1) throws CertificateException {
    }
    public X509Certificate[] getAcceptedIssuers() {
     return new X509Certificate[0];
    }
   } }, null);
   final SSLSocketFactory factory = new SSLSocketFactory(sslContext,
     new AllowAllHostnameVerifier());
   final Scheme https = new Scheme("https", 443, factory);
   final SchemeRegistry schemeRegistry = mClient
     .getConnectionManager().getSchemeRegistry();
   schemeRegistry.register(https);
  }
  catch (Exception e)
  {
  }
 }

0 件のコメント:

コメントを投稿