volleyにhttpsをサポートさせる
1935 ワード
git clone https://android.googlesource.com/platform/frameworks/volley
toolboxにツールクラスを追加する:HTTPSTrustManager.java
次にHurlStackを修正する.JAvaでのcreateConnectionメソッド.
最後にjarパッケージにします.
toolboxにツールクラスを追加する:HTTPSTrustManager.java
/**
* @author HuangWenwei
*
* @date 2014 8 20
*/
public class HTTPSTrustManager implements X509TrustManager {
private static TrustManager[] trustManagers;
private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
throws CertificateException {
}
public boolean isClientTrusted(X509Certificate[] chain){
return true;
}
public boolean isServerTrusted(X509Certificate[] chain){
return true;
}
public static void allowAllowSSL(){
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
SSLContext context = null;
if(trustManagers == null){
trustManagers = new TrustManager[]{
new HTTPSTrustManager()
};
}
try{
context = SSLContext.getInstance("TLS");
context.init(null, trustManagers, new SecureRandom());
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(KeyManagementException e){
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return _AcceptedIssuers;
}
}
次にHurlStackを修正する.JAvaでのcreateConnectionメソッド.
protected HttpURLConnection createConnection(URL url) throws IOException {
if("https".equals(url.getProtocol().toLowerCase())){
HTTPSTrustManager.allowAllowSSL();
}
return (HttpURLConnection) url.openConnection();
}
最後にjarパッケージにします.