From 6ade459c403f933a4da032d532f88da63a03b94c Mon Sep 17 00:00:00 2001 From: khy <18814188563@163.com> Date: Sat, 8 Oct 2016 14:36:21 +0800 Subject: [PATCH] =?UTF-8?q?https=E5=BF=BD=E7=95=A5=E8=AF=81=E4=B9=A6?= =?UTF-8?q?=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/gh/common/util/HttpsUtils.java | 56 +++++++++++++++++++ .../java/com/gh/download/DownloadThread.java | 14 ++++- 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/gh/common/util/HttpsUtils.java diff --git a/app/src/main/java/com/gh/common/util/HttpsUtils.java b/app/src/main/java/com/gh/common/util/HttpsUtils.java new file mode 100644 index 0000000000..ea5a2f6a75 --- /dev/null +++ b/app/src/main/java/com/gh/common/util/HttpsUtils.java @@ -0,0 +1,56 @@ +package com.gh.common.util; + +import java.net.URL; +import java.security.SecureRandom; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +/** + * Created by khy on 2016/10/8. + */ +public class HttpsUtils { + + private static class MyTrustManager implements 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 null; + } + + } + + private static class MyHostnameVerifier implements HostnameVerifier { + @Override + public boolean verify(String hostname, SSLSession session) { + return true; + } + + } + + public static HttpsURLConnection getHttpsURLConnection(URL url) throws Exception { + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + SSLContext sc = SSLContext.getInstance("TLS"); + sc.init(null, new TrustManager[]{new MyTrustManager()}, new SecureRandom()); + connection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + connection.setDefaultHostnameVerifier(new MyHostnameVerifier()); + + return connection; + } +} diff --git a/app/src/main/java/com/gh/download/DownloadThread.java b/app/src/main/java/com/gh/download/DownloadThread.java index 8b47465a42..64b410c120 100644 --- a/app/src/main/java/com/gh/download/DownloadThread.java +++ b/app/src/main/java/com/gh/download/DownloadThread.java @@ -3,6 +3,7 @@ package com.gh.download; import android.content.Context; import android.text.TextUtils; +import com.gh.common.util.HttpsUtils; import com.gh.common.util.Utils; import org.apache.http.HttpStatus; @@ -53,7 +54,15 @@ public class DownloadThread extends Thread { } URL url = new URL(entry.getUrl()); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + Utils.log("url = " + entry.getUrl()); + + HttpURLConnection connection = null; + if ("https".equals(url.getProtocol())) { + connection = HttpsUtils.getHttpsURLConnection(url); + } else { + connection = (HttpURLConnection) url.openConnection(); + } + connection.setRequestMethod("GET"); connection.setConnectTimeout(CONNECT_TIME); connection.setReadTimeout(READ_TIME); @@ -63,8 +72,9 @@ public class DownloadThread extends Thread { "startPosition-->" + targetFile.length()); //设置自动重定向 connection.setInstanceFollowRedirects(true); - + int code = connection.getResponseCode(); + Utils.log("code = " +code); if (code == HttpStatus.SC_MOVED_PERMANENTLY || code == HttpStatus.SC_MOVED_TEMPORARILY) { //未自动重定向