[英]How to access all HTTP Response Headers
I have a simple mobile app in Titanium that I'm using to debug the ability to log into our user system. 我在Titanium中有一个简单的移动应用程序,用于调试登录到用户系统的功能。
At the moment, I cannot seem to see the Set-Cookie
response header as it's always returned as null
. 目前,我似乎看不到
Set-Cookie
响应标头,因为它始终以null
返回。
I'm currently using Titanium SDK 1.7.5 (1.8 is horribly broken). 我目前正在使用Titanium SDK 1.7.5(1.8严重损坏)。
My code is very simple, a text book example of using the HTTPClient: 我的代码很简单,一个使用HTTPClient的教科书示例:
var loginReq = Titanium.Network.createHTTPClient();
var url = 'https://auth.csu.edu.au/login/login.pl';
var targetURL = 'http://my.csu.edu.au'
loginButton.addEventListener('click',function(e)
{
if (username.value != '' && password.value != '')
{
loginReq.open('POST', url);
Ti.API.info('Sending HTTP Request.');
var params = {
username: username.value,
password: password.value,
url: targetURL
}
loginReq.send(params);
}
else {
alert("Username/Password are required");
}
});
loginReq.onload = function() {
var cookie = loginReq.getResponseHeader('Set-Cookie');
Ti.API.info('Response Status: ' + loginReq.status);
Ti.API.info('Response Header - Cookie: ' + cookie);
Ti.API.info('Response Header - Location: ' + loginReq.getLocation());
if (Ti.Platform.osname !== 'android')
Ti.API.info('Headers: ' + JSON.stringify(loginReq.getResponseHeaders()));
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'test.html');
f.write(this.responseText);
var webview = Ti.UI.createWebView();
webview.url = f.nativePath;
var newWindow = Ti.UI.createWindow();
newWindow.add(webview);
newWindow.open({modal:true});
};
The output is as follows: 输出如下:
[INFO] Sending HTTP Request.
[INFO] Response Status: 200
[INFO] Response Header - Cookie: null
[INFO] Response Header - Location: https://auth.csu.edu.au/login/login.pl?redirect=true&url=http%3a%2f%2fmy%2ecsu%2eedu%2eau
[INFO] Headers: {"Connection":"Keep-Alive","Transfer-Encoding":"Identity","Keep-Alive":"timeout=5, max=99","Content-Type":"text/html","Server":"Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.7d mod_apreq2-20051231/2.6.0 mod_perl/2.0.4 Perl/v5.8.4","Date":"Thu, 02 Feb 2012 01:45:29 GMT"}
I'm just going around and around in circles as I can't seem to see what is exactly wrong here. 我只是绕圈走动,因为我似乎看不到这里到底有什么问题。 What confuses me is that
HTTPClient.getResponseHeaders()
is not even documented ( Titanium.Network.HTTPClient-object.html ) - and doesn't work for Android. 令我感到困惑的是,甚至没有记载
HTTPClient.getResponseHeaders()
( Titanium.Network.HTTPClient-object.html )-不适用于Android。
I know there must be something there because the webview displays the authenticated page fine (you can't get there unless you're authorised + cookie). 我知道那里一定有东西,因为Webview可以很好地显示经过身份验证的页面(除非获得授权+ Cookie,否则您无法到达那里)。
How can I get a full list of the headers to make sure I'm getting all the headers I'm supposed to? 如何获得标头的完整列表,以确保获得所有应有的标头?
I've found the answer to my own question. 我找到了自己问题的答案。
What I have in my code to return all headers is correct. 我在代码中返回所有标头的内容是正确的。 Using
HTTPClient.getResponseHeaders()
is the correct method for iOS and HTTPClient.getAllResponseHeaders()
for Android (no idea why there's two different ways - that could be a question for another day). 使用
HTTPClient.getResponseHeaders()
是iOS和正确的方法HTTPClient.getAllResponseHeaders()
的Android(不知道为什么有两种不同的方式-这可能是另一天的一个问题)。
The reason I'm not seeing the cookie header is because of a bug in Titanium 1.7.5 (and still exists in 1.8.1). 我没有看到Cookie标头的原因是由于Titanium 1.7.5中的一个错误(仍然存在于1.8.1中)。 It's not forwarding on the cookie on a 302 redirect.
它不是在302重定向上的Cookie上转发。
Jiras on the issues: Jiras的问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.