|
|
|
@ -25,6 +25,9 @@ import androidx.annotation.Nullable; |
|
|
|
|
*/ |
|
|
|
|
public class HttpManager implements IHttpManager { |
|
|
|
|
|
|
|
|
|
private static final int HTTP_TEMP_REDIRECT = 307; |
|
|
|
|
private static final int HTTP_PERM_REDIRECT = 308; |
|
|
|
|
|
|
|
|
|
private static final int DEFAULT_TIME_OUT = 20000; |
|
|
|
|
|
|
|
|
|
private int mTimeout; |
|
|
|
@ -92,57 +95,53 @@ public class HttpManager implements IHttpManager { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected File doInBackground(Void... voids) { |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
HttpsURLConnection.setDefaultSSLSocketFactory(SSLSocketFactoryUtils.createSSLSocketFactory()); |
|
|
|
|
HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactoryUtils.createTrustAllHostnameVerifier()); |
|
|
|
|
HttpURLConnection connect = (HttpURLConnection)new URL(url).openConnection(); |
|
|
|
|
connect.setRequestMethod("GET"); |
|
|
|
|
connect.setRequestProperty("Accept-Encoding", "identity"); |
|
|
|
|
private File download(String url) throws Exception{ |
|
|
|
|
HttpURLConnection connect = (HttpURLConnection)new URL(url).openConnection(); |
|
|
|
|
connect.setRequestMethod("GET"); |
|
|
|
|
connect.setRequestProperty("Accept-Encoding", "identity"); |
|
|
|
|
|
|
|
|
|
connect.setReadTimeout(mTimeout); |
|
|
|
|
connect.setConnectTimeout(mTimeout); |
|
|
|
|
connect.setReadTimeout(mTimeout); |
|
|
|
|
connect.setConnectTimeout(mTimeout); |
|
|
|
|
|
|
|
|
|
if(requestProperty!=null){ |
|
|
|
|
for(Map.Entry<String,String> entry : requestProperty.entrySet()){ |
|
|
|
|
connect.setRequestProperty(entry.getKey(),entry.getValue()); |
|
|
|
|
} |
|
|
|
|
if(requestProperty!=null){ |
|
|
|
|
for(Map.Entry<String,String> entry : requestProperty.entrySet()){ |
|
|
|
|
connect.setRequestProperty(entry.getKey(),entry.getValue()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
connect.connect(); |
|
|
|
|
int responseCode = connect.getResponseCode(); |
|
|
|
|
Log.d(Constants.TAG,"Content-Type:" + connect.getContentType()); |
|
|
|
|
if(responseCode == HttpURLConnection.HTTP_OK){ |
|
|
|
|
connect.connect(); |
|
|
|
|
|
|
|
|
|
Log.d(Constants.TAG,"Content-Type:" + connect.getContentType()); |
|
|
|
|
int responseCode = connect.getResponseCode(); |
|
|
|
|
switch (responseCode){ |
|
|
|
|
case HttpURLConnection.HTTP_OK: { |
|
|
|
|
InputStream is = connect.getInputStream(); |
|
|
|
|
|
|
|
|
|
long length = connect.getContentLength(); |
|
|
|
|
|
|
|
|
|
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
|
|
|
|
length = connect.getContentLengthLong(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Log.d(Constants.TAG,"contentLength:" + length); |
|
|
|
|
Log.d(Constants.TAG, "contentLength:" + length); |
|
|
|
|
|
|
|
|
|
long progress = 0; |
|
|
|
|
|
|
|
|
|
byte[] buffer = new byte[8192]; |
|
|
|
|
|
|
|
|
|
int len; |
|
|
|
|
File file = new File(path,filename); |
|
|
|
|
File file = new File(path, filename); |
|
|
|
|
FileOutputStream fos = new FileOutputStream(file); |
|
|
|
|
while ((len = is.read(buffer)) != -1){ |
|
|
|
|
if(isCancel){ |
|
|
|
|
while ((len = is.read(buffer)) != -1) { |
|
|
|
|
if (isCancel) { |
|
|
|
|
cancel(true); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
fos.write(buffer,0,len); |
|
|
|
|
fos.write(buffer, 0, len); |
|
|
|
|
progress += len; |
|
|
|
|
//更新进度
|
|
|
|
|
if(length>0){ |
|
|
|
|
publishProgress(progress,length); |
|
|
|
|
if (length > 0) { |
|
|
|
|
publishProgress(progress, length); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -153,10 +152,31 @@ public class HttpManager implements IHttpManager { |
|
|
|
|
connect.disconnect(); |
|
|
|
|
|
|
|
|
|
return file; |
|
|
|
|
}else {//连接失败
|
|
|
|
|
throw new ConnectException(String.format("responseCode = %d",responseCode)); |
|
|
|
|
} |
|
|
|
|
case HttpURLConnection.HTTP_MULT_CHOICE: |
|
|
|
|
case HttpURLConnection.HTTP_MOVED_PERM: |
|
|
|
|
case HttpURLConnection.HTTP_MOVED_TEMP: |
|
|
|
|
case HttpURLConnection.HTTP_SEE_OTHER: |
|
|
|
|
case HTTP_TEMP_REDIRECT: |
|
|
|
|
case HTTP_PERM_REDIRECT: {//重定向
|
|
|
|
|
String redirectUrl = connect.getHeaderField("Location"); |
|
|
|
|
Log.d(Constants.TAG,"redirectUrl = " + redirectUrl); |
|
|
|
|
connect.disconnect(); |
|
|
|
|
return download(redirectUrl); |
|
|
|
|
} |
|
|
|
|
default://连接失败
|
|
|
|
|
throw new ConnectException(String.format("responseCode = %d",responseCode)); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected File doInBackground(Void... voids) { |
|
|
|
|
|
|
|
|
|
try{ |
|
|
|
|
HttpsURLConnection.setDefaultSSLSocketFactory(SSLSocketFactoryUtils.createSSLSocketFactory()); |
|
|
|
|
HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactoryUtils.createTrustAllHostnameVerifier()); |
|
|
|
|
return download(url); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
this.exception = e; |
|
|
|
|
e.printStackTrace(); |
|
|
|
@ -197,7 +217,6 @@ public class HttpManager implements IHttpManager { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onCancelled() { |
|
|
|
|
super.onCancelled(); |
|
|
|
|