|
|
@ -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,10 +109,11 @@ 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(); |
|
|
@ -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(); |
|
|
|