private
static
final
String KEY_STORE_TYPE_BKS =
"bks"
;
private
static
final
String KEY_STORE_TYPE_P12 =
"PKCS12"
;
private
static
final
String KEY_STORE_CLIENT_PATH =
"client.p12"
;
private
static
final
String KEY_STORE_TRUST_PATH =
"client.truststore"
;
private
static
final
String KEY_STORE_PASSWORD =
"123456"
;
private
static
final
String KEY_STORE_TRUST_PASSWORD =
"123456"
;
/** * 获取SSLContext * * @param context 上下文 * @return SSLContext */
private
static
SSLContext getSSLContext(Context context) {
try
{
KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE_P12);
KeyStore trustStore = KeyStore.getInstance(KEY_STORE_TYPE_BKS);
InputStream ksIn = context.getResources().getAssets().open(KEY_STORE_CLIENT_PATH);
InputStream tsIn = context.getResources().getAssets().open(KEY_STORE_TRUST_PATH);
try
{
keyStore.load(ksIn, KEY_STORE_PASSWORD.toCharArray());
trustStore.load(tsIn, KEY_STORE_TRUST_PASSWORD.toCharArray());
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
try
{
ksIn.close();
}
catch
(Exception ignore) {
}
try
{
tsIn.close();
}
catch
(Exception ignore) {
}
}
SSLContext sslContext = SSLContext.getInstance(
"TLS"
);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
"X509"
);
keyManagerFactory.init(keyStore, KEY_STORE_PASSWORD.toCharArray());
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(),
null
);
return
sslContext;
}
catch
(Exception e) {
Log.e(
"tag"
, e.getMessage(), e);
}
return
null
;
}
/** * 获取SSL认证需要的HttpClient * * @param context 上下文 * @return OkHttpClient */
public
static
OkHttpClient getSSLContextHttp(Context context) {
OkHttpClient client =
new
OkHttpClient();
SSLContext sslContext = getSSLContext(context);
if
(sslContext !=
null
) {
client.setSslSocketFactory(sslContext.getSocketFactory());
}
return
client;
}
/** * 获取HttpsURLConnection * * @param context 上下文 * @param url 连接url * @param method 请求方式 * @return HttpsURLConnection */
public
static
HttpsURLConnection getHttpsURLConnection(Context context, String url, String method) {
URL u;
HttpsURLConnection connection =
null
;
try
{
SSLContext sslContext = getSSLContext(context);
if
(sslContext !=
null
) {
u =
new
URL(url);
connection = (HttpsURLConnection) u.openConnection();
connection.setRequestMethod(method);
connection.setDoOutput(
true
);
connection.setDoInput(
true
);
connection.setUseCaches(
false
);
connection.setRequestProperty(
"Content-Type"
,
"binary/octet-stream"
);
connection.setSSLSocketFactory(sslContext.getSocketFactory());
connection.setConnectTimeout(
30000
);
}
}
catch
(Exception e) {
e.printStackTrace();
}
return
connection;
}