多次 createHTTPClient Titanium 调用导致 Cookie 损坏 [英] Cookie corruption with multiple createHTTPClient Titanium calls

查看:19
本文介绍了多次 createHTTPClient Titanium 调用导致 Cookie 损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Appcelerator 的 Titanium 中创建一个涉及 webView 和后台调用的 Android 应用程序时,我遇到了一个问题/错误,即 cookie 在多次 createHTTPClient 调用中被破坏.

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 最初是从 webView 中获取的:

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});

然后用于后台调用:

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(); 

第一次调用上面的 createHTTPClient 代码块时,一切正常,但是上面代码的后续运行会发送损坏的 cookie.在 Google App Engine (gae) 中,打印出的请求标头如下所示(已损坏):

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)

损坏的响应(仅显示请求标头的 cookie 部分)

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

<代码>曲奇:AUTH = eyJfdXNlciI6WzYsMSwiVmRoZEtnYWZRMnNxazFjaVM0U1lKdCIsMTM1NzIyMzcwOSwxMzU3MjIzNzA5XX0 \ 075 | 1357223709 | 4f622167f477a8c82cab196af4b0029af1a966d7",AUTH = eyJfdXNlciI6WzYsMSwiVmRoZEtnYWZRMnNxazFjaVM0U1lKdCIsMTM1NzIyMzcwOSwxMzU3MjIzNzA5XX0 \ 075 | 1357225569 | 7a469fab7a38a437649c25620729e07c4607f617Cookie2:$Version=1

工作回复

Cookie: auth="eyJfdXNlciI6WzYsMSwiVmRoZEtnYWZRMnNxazFjaVM0U1lKdCIsMTM1NzIyMzcwOSwxMzU3MjIzNzA5XX0\075|1357223642709acode28fc7209acode28fc72075c618c618c618c618c618c617fc97afc96fc617fc615c61fc617fc697fc97fc697fc697fc97fc697fc697fc9617fc697fc617fc617fc617fc617fc69707...

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

我怀疑该问题与 unicode 字符或 createHTTPClient 中的某些内容有关.损坏的 cookie 中显示了两个 auth= 语句.

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

总而言之,当应用首次启动时,后台 Titanium.Network.createHTTPClient 调用有效,之后的任何调用似乎都会发送损坏的 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.

推荐答案

HTTPClient 文档 说对象旨在用于单个请求",所以我假设在多次调用后一切都会重置.但是在第一次通话之后就有些不同了.

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.

在设置 cookie 之前将 loader.clearCookies(base_url); 添加到代码中似乎可以解决问题.

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(); 

这篇关于多次 createHTTPClient Titanium 调用导致 Cookie 损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆