Add support for following 301/302 redirects on android

This commit is contained in:
Phil Rha 2016-05-06 15:16:06 -07:00
parent 01d43a011a
commit 5d1a201a0c
1 changed files with 18 additions and 0 deletions

View File

@ -50,6 +50,24 @@ public class Downloader extends AsyncTask<DownloadParams, int[], DownloadResult>
int statusCode = connection.getResponseCode();
int lengthOfFile = connection.getContentLength();
boolean isRedirect = (
statusCode != HttpURLConnection.HTTP_OK &&
(
statusCode == HttpURLConnection.HTTP_MOVED_PERM ||
statusCode == HttpURLConnection.HTTP_MOVED_TEMP
)
);
if (isRedirect) {
String redirectURL = connection.getHeaderField("Location");
connection = (HttpURLConnection) new URL(redirectURL).openConnection();
connection.setConnectTimeout(5000);
connection.connect();
statusCode = connection.getResponseCode();
lengthOfFile = connection.getContentLength();
}
Map<String, List<String>> headers = connection.getHeaderFields();
Map<String, String> headersFlat = new HashMap<String, String>();