超文本傳輸協議(HTTP,HyperText Transfer Protocol),默認端口:80html
2012年google提出的SPDY方案,主要解決:java
能夠當作是SPDY的升級版。android
(Hyper Text Transfer Protocol over Secure Socket Layer),是以安全爲目標的HTTP通道,即HTTP下加入SSL層,端口:443算法
官方推薦:Android推薦方案瀏覽器
/**
* HTTPS未知的證書頒發機構處理方法
* Android客戶端存儲證書
*
* @param input 待信任的CA證書流
* @return SSLContext
*/
public static SSLContext getSSLContext(InputStream input) {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate ca = cf.generateCertificate(input);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
sslContext.init(null, tmf.getTrustManagers(), null);
input.close();
return sslContext;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return null;
}
複製代碼
/**
* 單獨使用SSL + HTTP時
* @param trustManagers
* @return
*/
public static SSLContext getSSLContext(TrustManager[] trustManagers) {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, null);
return sslContext;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return null;
}
public static TrustManager[] sDefaultTrustManagers = new TrustManager[] {new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// TODO: 2018/1/23雙向校驗中,向服務端發客戶端證書
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// TODO: 2018/1/23 雙向校驗中,校驗服務端證書
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}};
public static HostnameVerifier sHostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// 不驗證主機名
return true;
}
};
複製代碼