可以完成的任务,也使用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;
}