支持重定向 (#7)

android
jenly1314 5 years ago
parent 32e71c030e
commit 888ce12ddc
  1. 101
      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,51 +109,73 @@ 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){
InputStream is = connect.getInputStream(); 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(); 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;
byte[] buffer = new byte[8192];
int len;
File file = new File(path, filename);
FileOutputStream fos = new FileOutputStream(file);
while ((len = is.read(buffer)) != -1) {
if (isCancel) {
cancel(true);
break;
}
fos.write(buffer, 0, len);
progress += len;
//更新进度
if (length > 0) {
publishProgress(progress, length);
}
}
long progress = 0; fos.flush();
fos.close();
is.close();
byte[] buffer = new byte[8192]; connect.disconnect();
int len; return file;
File file = new File(path,filename);
FileOutputStream fos = new FileOutputStream(file);
while ((len = is.read(buffer)) != -1){
if(isCancel){
cancel(true);
break;
}
fos.write(buffer,0,len);
progress += len;
//更新进度
if(length>0){
publishProgress(progress,length);
}
} }
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));
fos.flush();
fos.close();
is.close();
connect.disconnect();
return file;
}else {//连接失败
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