Skip to content

Commit 05f9da3

Browse files
committed
update OtherUtils
1 parent f542d7f commit 05f9da3

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

library/src/com/lidroid/xutils/bitmap/download/DefaultDownloader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public long downloadToStream(String uri, OutputStream outputStream, final Bitmap
4242
URLConnection urlConnection = null;
4343
BufferedInputStream bis = null;
4444

45-
OtherUtils.trustAllSSLForHttpsURLConnection();
45+
OtherUtils.trustAllHttpsURLConnection();
4646

4747
long result = -1;
4848
long fileLen = 0;

library/src/com/lidroid/xutils/util/OtherUtils.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@
2727
import org.apache.http.client.methods.HttpRequestBase;
2828
import org.apache.http.protocol.HTTP;
2929

30-
import javax.net.ssl.HttpsURLConnection;
31-
import javax.net.ssl.SSLContext;
32-
import javax.net.ssl.TrustManager;
33-
import javax.net.ssl.X509TrustManager;
30+
import javax.net.ssl.*;
3431
import java.io.File;
3532
import java.io.UnsupportedEncodingException;
3633
import java.lang.reflect.Field;
@@ -228,32 +225,39 @@ public static StackTraceElement getCallerStackTraceElement() {
228225
return Thread.currentThread().getStackTrace()[4];
229226
}
230227

231-
private static TrustManager[] trustAllCerts;
228+
private static SSLSocketFactory sslSocketFactory;
232229

233-
public static void trustAllSSLForHttpsURLConnection() {
230+
public static void trustAllHttpsURLConnection() {
234231
// Create a trust manager that does not validate certificate chains
235-
if (trustAllCerts == null) {
236-
trustAllCerts = new TrustManager[]{new X509TrustManager() {
232+
if (sslSocketFactory == null) {
233+
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
234+
@Override
237235
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
238236
return null;
239237
}
240238

241-
public void checkClientTrusted(X509Certificate[] certs, String authType) {
239+
@Override
240+
public void checkClientTrusted(X509Certificate[] certs,
241+
String authType) {
242242
}
243243

244-
public void checkServerTrusted(X509Certificate[] certs, String authType) {
244+
@Override
245+
public void checkServerTrusted(X509Certificate[] certs,
246+
String authType) {
245247
}
246248
}};
249+
try {
250+
SSLContext sslContext = SSLContext.getInstance("TLS");
251+
sslContext.init(null, trustAllCerts, null);
252+
sslSocketFactory = sslContext.getSocketFactory();
253+
} catch (Throwable e) {
254+
LogUtils.e(e.getMessage(), e);
255+
}
247256
}
248-
// Install the all-trusting trust manager
249-
final SSLContext sslContext;
250-
try {
251-
sslContext = SSLContext.getInstance("TLS");
252-
sslContext.init(null, trustAllCerts, null);
253-
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
254-
} catch (Throwable e) {
255-
LogUtils.e(e.getMessage(), e);
257+
258+
if (sslSocketFactory != null) {
259+
HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);
260+
HttpsURLConnection.setDefaultHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
256261
}
257-
HttpsURLConnection.setDefaultHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
258262
}
259263
}

0 commit comments

Comments
 (0)