Cookie corruption with multiple createHTTPClient Titanium calls

女生的网名这么多〃 提交于 2019-12-25 06:26:04

问题


While creating an Android app in Appcelerator's Titanium that involves both webView and background calls, I ran into a problem / bug where the cookies were getting corrupted on multiple createHTTPClient calls.

Cookies were originally obtained from the webView:

var webview = Ti.UI.createWebView();
webview.url = 'http://www.example.com';
window.add(webview);
webview.addEventListener('load', function(e) {
    cookies = e.source.evalJS("document.cookie");
    Titanium.App.Properties.setString('cookies',cookies);
}
window.open({modal:true});

and then later used with a background call:

var loader = Titanium.Network.createHTTPClient();  
loader.open("GET",base_url + url); 
loader.onload = function() {
    // process response
}
loader.setRequestHeader('Cookie',Titanium.App.Properties.getString("cookies"));
loader.send(); 

The first time the above createHTTPClient chunk of code was called, everything worked, but subsequent runs of the above code would send corrupted cookies. In Google App Engine (gae), printing out the request headers would look like this (broken):

logging.info('Request:\n%s' % self.request)

broken response (only the cookie portion of the request header is shown)

Cookie: auth="eyJfdXNlciI6WzYsMSwiVmRoZEtnYWZRMnNxazFjaVM0U1lKdCIsMTM1NzIyMzcwOSwxMzU3MjIzNzA5XX0\075|1357223709|4f622167f477a8c82cab196af4b0029af1a966d7", auth=eyJfdXNlciI6WzYsMSwiVmRoZEtnYWZRMnNxazFjaVM0U1lKdCIsMTM1NzIyMzcwOSwxMzU3MjIzNzA5XX0\075|1357225569|7a469fab7a38a437649c25620729e07c4607f617 Cookie2: $Version=1

working response

Cookie: auth="eyJfdXNlciI6WzYsMSwiVmRoZEtnYWZRMnNxazFjaVM0U1lKdCIsMTM1NzIyMzcwOSwxMzU3MjIzNzA5XX0\075|1357223709|4f622167f477a8c82cab196af4b0029af1a966d7" ...

I suspect the issue has something to do with unicode characters or something inside createHTTPClient. Two auth= statements are shown in the corrupted cookie.

In summary, when the app first launches, the background Titanium.Network.createHTTPClient call works, and any calls after that appear to send corrupted cookies.


回答1:


The HTTPClient documentation says "object is intended to be used for a single request," so I assumed everything would reset after multiple calls. But something was different after the first call.

Adding loader.clearCookies(base_url); to the code before setting the cookies seems to fix the issue.

var loader = Titanium.Network.createHTTPClient();  
loader.open("GET",base_url + url); 
loader.onload = function() {
    // process response
}
loader.clearCookies(base_url); // THE FIX.
loader.setRequestHeader('Cookie',Titanium.App.Properties.getString("cookies"));
loader.send(); 


来源:https://stackoverflow.com/questions/14141746/cookie-corruption-with-multiple-createhttpclient-titanium-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!