OKHttp的常用配置如配置Https证书,忽略Https证书,Cookie持久化

Retrofit应该是目前最火的联网框架,但是内部还是使用的OKHttp,在这总结一下常用的配置。

  1. 做Cookie的持久化。 OKHttp3.0之后和之前做Cookie持久化有了点区别下面直接上代码:
.cookieJar(new CookieJar() { 
private final HashMap> cookieStore = new HashMap<>(); 
@Override 
public void saveFromResponse(HttpUrl url, List cookies) { cookieStore.put(url.host(), cookies); } 
@Override 
public List loadForRequest(HttpUrl url) {
 List cookies = cookieStore.get(url.host()); 
return cookies != null ? cookies : new ArrayList(); } })```
2.添加Https证书 - 初始化SSLContext 

public static void setCard(InputStream certificate) {
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null);
String certificateAlias = Integer.toString(0); keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(certificate));
sslContext = SSLContext.getInstance("TLS");
final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
sslContext.init ( null, trustManagerFactory.getTrustManagers(), new SecureRandom() );
} catch (CertificateException e)
{
e.printStackTrace();
}
catch (KeyStoreException e) {
e.printStackTrace(); }
catch (NoSuchAlgorithmException e)
{ e.printStackTrace(); }
catch (IOException e) {
e.printStackTrace(); }
catch (KeyManagementException e)
{ e.printStackTrace(); } }```

  • 通过构造者模式添加至OKHttpClient
.sslSocketFactory(sslContext.getSocketFactory()) .hostnameVerifier(new HostnameVerifier() { 
@Override
 public boolean verify(String hostname, SSLSession session) { 
return true; } })

3 . 忽略Https证书

/** * 忽略所有https证书 */
 private void overlockCard() {
 final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() { 
@Override
 public void checkClientTrusted( 
java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { }
 @Override 
public void checkServerTrusted( java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { }
 @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
X509Certificate[] x509Certificates = new X509Certificate[0]; return x509Certificates; } }};
 try {
 sslContext = SSLContext.getInstance("SSL"); 
sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); } 
catch (Exception e) {
 LogUtil.e("ssl出现异常"); } }```
最终关于整个Okhttp的配置模块:

private static final String CER_NAME = ""; //https签名证书name
if (StringUtils.isEmpty(CER_NAME)) {
//忽略所有证书
overlockCard(); } else {
//选择证书
try {
setCard(UIUtils.getContext().getAssets().open(CER_NAME)); }
catch (IOException e) {
e.printStackTrace(); } }
okHttpClient = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS) .readTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .sslSocketFactory(sslContext.getSocketFactory()) .hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true; } }) .cookieJar(new CookieJar() {
private final HashMap> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl url, List cookies) { cookieStore.put(url.host(), cookies); }
@Override
public List loadForRequest(HttpUrl url) {
List cookies = cookieStore.get(url.host());
return cookies != null ? cookies : new ArrayList(); } }) .build();```

你可能感兴趣的:(OKHttp的常用配置如配置Https证书,忽略Https证书,Cookie持久化)