[TIMOB-20219] Android: HTTPClient not working with http redirected to https
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Critical |
Status | Closed |
Resolution | Duplicate |
Resolution Date | 2016-02-11T00:00:56.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Release 5.4.0 |
Components | Android |
Labels | redirect |
Reporter | Shawn Lan |
Assignee | Hieu Pham |
Created | 2016-01-07T20:31:03.000+0000 |
Updated | 2016-09-23T18:07:47.000+0000 |
Description
If the request url is from http redirected to https, HTTPClient downloads garbage instead. On iOS it's working fine.
In the following example, http://audio.forerunner.cc/test.pdf is permanently redirected to https://audio.forerunner.cc/test.pdf
Examine the downloaded file and see it's just garbage. Replace the url with https://audio.forerunner.cc/test.pdf. It will then download the file correctly.
index.js
var opt = {url:'http://audio.forerunner.cc/test.pdf',type:'GET',file:Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory,'test.pdf')};
require('http').request(opt);
http.js
exports.request = function(opts) {
var xhr = Ti.Network.createHTTPClient();
xhr.timeout = (opts.timeout) ? opts.timeout : 5000;
xhr.enableKeepAlive = (opts.keepAlive) ? opts.keepAlive : false;
xhr.onload = function() {
if(opts.file && OS_IOS) opts.file.remoteBackup = false;
var data = (opts.format === 'json') ? JSON.parse(this.responseText) : this.responseData;
var rHeader = opts.rHeader ? this.getResponseHeader(opts.rHeader) : null;
if((data || rHeader) && opts.callback)
opts.callback(data, rHeader);
};
if (opts.ondatastream)
xhr.ondatastream = function(e){
opts.ondatastream(e.progress);
};
xhr.onerror = function() {
if(opts.file)
opts.file.deleteFile();
if(opts.onerror)
opts.onerror();
};
xhr.open(opts.type?opts.type:'GET', encodeURI(opts.url));
if(opts.file) xhr.file = OS_ANDROID?opts.file.nativePath:opts.file;
if(opts.headers) {
for(var i = 0, j = opts.headers.length; i < j; i++) {
xhr.setRequestHeader( opts.headers[i].name, opts.headers[i].value );
}
}
if(opts.data) {
xhr.send(JSON.stringify(opts.data));
} else {
xhr.send(null);
}
};
Even, We are facing the same issue. With ti 4.0.0 sdk, it works fine both on iOS and Android. But with 5.1.1 ,It doesn't work on android. We get a garbage file downloaded, and pdf viewer gives a message "This Document cannot be opened". Same result when we try to download .xls file. Kindly , provide a workaround with this sdk. Thanks.
Closing as duplicate & the original ticket is closed.