配置证书相关信息
.package com.example.qundui.learndemo.netutils;
/**
* Created by qundui on 2017/4/24.
*/
import android.annotation.SuppressLint;
import com.example.qundui.learndemo.Myapp;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
public class SSLContextUtil {
/**
* 拿到https证书, SSLContext 。
*/
@SuppressLint("TrulyRandom")
public static SSLContext getSSLContext() {
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
InputStream inputStream = Myapp.getInstance().getAssets().open( "srca.cer");
CertificateFactory cerFactory = CertificateFactory.getInstance("X.509");
Certificate cer = cerFactory.generateCertificate(inputStream);
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(null, null);
keyStore.setCertificateEntry("trust", cer);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, null);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom());
} catch (Exception e) {
e.printStackTrace();
}
return sslContext;
}
/**
* 如果不需要https证书
*/
public static SSLContext getDefaultSLLContext() {
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{trustManagers}, new SecureRandom());
} catch (Exception e) {
e.printStackTrace();
}
return sslContext;
}
private static TrustManager trustManagers = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
};
public static final HostnameVerifier HOSTNAME_VERIFIER = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
}
请求操作
private void getData(String url1) {
try {
URL url = new URL(url1);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
// 设置SSLSocketFoactory,这里有两种:1.需要安全证书 2.不需要安全证书(绕过证书)
if (urlConnection instanceof HttpsURLConnection) { // 是Https请求
SSLContext sslContext = SSLContextUtil.getSSLContext();
if (sslContext != null) {
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslSocketFactory);
}
}
urlConnection.setConnectTimeout(8 * 1000);
urlConnection.setReadTimeout(8 * 1000);
int responseCode = urlConnection.getResponseCode();
if (responseCode == 200) { // 请求成功
InputStream inputStream = urlConnection.getInputStream();
Log.i(“gsc”,”—-“+StringTools.StringtoInpustream(inputStream));
inputStream.close();
}
urlConnection.disconnect();
}catch (Exception e){
}
}