支持重定向 (#7)

android
jenly1314 5 years ago
parent 32e71c030e
commit 888ce12ddc
  1. 41
      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 {
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;
@ -91,12 +94,7 @@ public class HttpManager implements IHttpManager {
}
@Override
protected File doInBackground(Void... voids) {
try {
HttpsURLConnection.setDefaultSSLSocketFactory(SSLSocketFactoryUtils.createSSLSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactoryUtils.createTrustAllHostnameVerifier());
private File download(String url) throws Exception{
HttpURLConnection connect = (HttpURLConnection)new URL(url).openConnection();
connect.setRequestMethod("GET");
connect.setRequestProperty("Accept-Encoding", "identity");
@ -111,10 +109,11 @@ public class HttpManager implements IHttpManager {
}
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();
long length = connect.getContentLength();
@ -152,10 +151,31 @@ public class HttpManager implements IHttpManager {
connect.disconnect();
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));
}
}
@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();
@ -196,7 +216,6 @@ public class HttpManager implements IHttpManager {
}
}
@Override
protected void onCancelled() {
super.onCancelled();

Loading…
Cancel
Save