Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-20219] Android: HTTPClient not working with http redirected to https

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionDuplicate
Resolution Date2016-02-11T00:00:56.000+0000
Affected Version/sn/a
Fix Version/sRelease 5.4.0
ComponentsAndroid
Labelsredirect
ReporterShawn Lan
AssigneeHieu Pham
Created2016-01-07T20:31:03.000+0000
Updated2016-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);
	}
};

Comments

  1. Parween Singh 2016-04-11

    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.
  2. Lokesh Choudhary 2016-06-24

    Closing as duplicate & the original ticket is closed.

JSON Source