Skip to content

Commit

Permalink
Merge pull request #7533 from hieupham007/timob-11863
Browse files Browse the repository at this point in the history
TIMOB-11863: Handle redirect from http to https (vice versa)
  • Loading branch information
ashcoding committed Jan 11, 2016
2 parents 0b4f4a9 + 9dd5a39 commit edc97fb
Showing 1 changed file with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,21 +376,32 @@ public Bitmap getBitmap(boolean needRetry, boolean densityScaled)
}
// If decoding fails, we try to get it from httpclient.
if (b == null) {
HttpURLConnection connection = null;
try {
URL mURL = new URL(url);
connection = (HttpURLConnection) mURL.openConnection();
connection.setInstanceFollowRedirects(true);
connection.setDoInput(true);
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
b = BitmapFactory.decodeStream(connection.getInputStream());
} else
b = null;
} catch (Exception e) {
b = null;
} finally {
HttpURLConnection connection = null;
try {
URL mURL = new URL(url);
connection = (HttpURLConnection) mURL.openConnection();
connection.setInstanceFollowRedirects(true);
connection.setDoInput(true);
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
b = BitmapFactory.decodeStream(connection.getInputStream());
} else if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
String location = connection.getHeaderField("Location");
URL nURL = new URL(location);
String prevProtocol = mURL.getProtocol();
//HttpURLConnection doesn't handle http to https redirects so we do it manually.
if (prevProtocol != null && !prevProtocol.equals(nURL.getProtocol())) {
b = BitmapFactory.decodeStream(nURL.openStream());
} else {
b = BitmapFactory.decodeStream(connection.getInputStream());
}
} else {
b = null;
}
} catch (Exception e) {
b = null;
} finally {
if (connection != null) {
connection.disconnect();
}
Expand Down

0 comments on commit edc97fb

Please sign in to comment.