android ssl http,Android SSL HTTP请求使用自签名证书和CA

可以完成的任务,也使用DefaultHttpClient,即使here is suggested到:

身高HttpURLConnection的新代码

讲究也进口或添加证书到您的应用程序,因为你可能有在证书过期时更新证书存在问题。

这里如何获得DefaultHttpClient信任自签名证书:

* This method returns the appropriate HttpClient.

* @param isTLS Whether Transport Layer Security is required.

* @param trustStoreInputStream The InputStream generated from the BKS keystore.

* @param trustStorePsw The password related to the keystore.

* @return The DefaultHttpClient object used to invoke execute(request) method.

private DefaultHttpClient getHttpClient(boolean isTLS, InputStream trustStoreInputStream, String trustStorePsw)

throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException, UnrecoverableKeyException {

DefaultHttpClient client = null;

SchemeRegistry schemeRegistry = new SchemeRegistry();

Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 8080);

schemeRegistry.register(http);

if(isTLS) {

KeyStore trustKeyStore = null;

char[] trustStorePswCharArray = null;

if(trustStorePsw!=null) {

trustStorePswCharArray = trustStorePsw.toCharArray();

}

trustKeyStore = KeyStore.getInstance("BKS");

trustKeyStore.load(trustStoreInputStream, trustStorePswCharArray);

SSLSocketFactory sslSocketFactory = null;

sslSocketFactory = new SSLSocketFactory(trustKeyStore);

Scheme https = new Scheme("https", sslSocketFactory, 8443);

schemeRegistry.register(https);

}

HttpParams httpParams = new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);

HttpConnectionParams.setSoTimeout(httpParams, SOCKET_TIMEOUT);

ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);

client = new DefaultHttpClient(clientConnectionManager, httpParams);

return client;

}

这里如何获得HttpsURLConnection:

* This method set the certificate for the HttpsURLConnection

* @param url The url to contact.

* @param certificateInputStream The InputStream generated from the .crt certificate.

* @param certAlias The alias for the certificate.

* @return The returned HttpsURLConnection

private HttpsURLConnection getHttpsURLConnection(URL url, InputStream certificateInputStream, String certAlias)

throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {

HttpsURLConnection connection = null;

CertificateFactory certFactory = null;

Certificate cert = null;

KeyStore keyStore = null;

TrustManagerFactory tmFactory = null;

SSLContext sslContext = null;

// Load certificates from an InputStream

certFactory = CertificateFactory.getInstance("X.509");

cert = certFactory.generateCertificate(certificateInputStream);

certificateInputStream.close();

// Create a KeyStore containing the trusted certificates

keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

keyStore.load(null, null);

keyStore.setCertificateEntry(certAlias, cert);

// Create a TrustManager that trusts the certificates in our KeyStore

tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

tmFactory.init(keyStore);

// Create an SSLContext that uses our TrustManager

sslContext = SSLContext.getInstance("TLS");

sslContext.init(null, tmFactory.getTrustManagers(), null);

connection = (HttpsURLConnection)url.openConnection();

connection.setSSLSocketFactory(sslContext.getSocketFactory());

return connection;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值