支持重定向 (#7)

android
jenly1314 5 years ago
parent 32e71c030e
commit 888ce12ddc
  1. 57
      app-updater/src/main/java/com/king/app/updater/http/HttpManager.java

@ -24,6 +24,9 @@ import javax.net.ssl.HttpsURLConnection;
*/ */
public class HttpManager implements IHttpManager { 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 static final int DEFAULT_TIME_OUT = 20000;
private int mTimeout; private int mTimeout;
@ -91,12 +94,7 @@ public class HttpManager implements IHttpManager {
} }
@Override private File download(String url) throws Exception{
protected File doInBackground(Void... voids) {
try {
HttpsURLConnection.setDefaultSSLSocketFactory(SSLSocketFactoryUtils.createSSLSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactoryUtils.createTrustAllHostnameVerifier());
HttpURLConnection connect = (HttpURLConnection)new URL(url).openConnection(); HttpURLConnection connect = (HttpURLConnection)new URL(url).openConnection();
connect.setRequestMethod("GET"); connect.setRequestMethod("GET");
connect.setRequestProperty("Accept-Encoding", "identity"); connect.setRequestProperty("Accept-Encoding", "identity");
@ -111,37 +109,38 @@ public class HttpManager implements IHttpManager {
} }
connect.connect(); connect.connect();
int responseCode = connect.getResponseCode();
Log.d(Constants.TAG,"Content-Type:" + connect.getContentType());
if(responseCode == HttpURLConnection.HTTP_OK){
Log.d(Constants.TAG,"Content-Type:" + connect.getContentType());
int responseCode = connect.getResponseCode();
switch (responseCode){
case HttpURLConnection.HTTP_OK: {
InputStream is = connect.getInputStream(); InputStream is = connect.getInputStream();
long length = connect.getContentLength(); 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(); length = connect.getContentLengthLong();
} }
Log.d(Constants.TAG,"contentLength:" + length); Log.d(Constants.TAG, "contentLength:" + length);
long progress = 0; long progress = 0;
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
int len; int len;
File file = new File(path,filename); File file = new File(path, filename);
FileOutputStream fos = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream(file);
while ((len = is.read(buffer)) != -1){ while ((len = is.read(buffer)) != -1) {
if(isCancel){ if (isCancel) {
cancel(true); cancel(true);
break; break;
} }
fos.write(buffer,0,len); fos.write(buffer, 0, len);
progress += len; progress += len;
//更新进度 //更新进度
if(length>0){ if (length > 0) {
publishProgress(progress,length); publishProgress(progress, length);
} }
} }
@ -152,10 +151,31 @@ public class HttpManager implements IHttpManager {
connect.disconnect(); connect.disconnect();
return file; return file;
}else {//连接失败 }
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)); 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) { } catch (Exception e) {
this.exception = e; this.exception = e;
e.printStackTrace(); e.printStackTrace();
@ -196,7 +216,6 @@ public class HttpManager implements IHttpManager {
} }
} }
@Override @Override
protected void onCancelled() { protected void onCancelled() {
super.onCancelled(); super.onCancelled();

Loading…
Cancel
Save