如何访问所有 HTTP 响应标头
我在 Titanium 中有一个简单的移动应用程序,我用它来调试登录用户系统的能力。
目前,我似乎看不到 Set-Cookie
响应标头,因为它始终返回为 null
。
我目前正在使用 Titanium SDK 1.7.5(1.8 严重损坏)。
我的代码非常简单,是使用 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});
};
输出如下:
[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"}
我只是在兜圈子,因为我似乎看不出这里到底出了什么问题。让我困惑的是 HTTPClient.getResponseHeaders()
甚至没有记录(Titanium.Network.HTTPClient-object.html) - 并且不适用于 Android。
我知道那里一定有什么东西,因为网络视图可以很好地显示经过身份验证的页面(除非您获得授权+ cookie,否则您无法到达那里)。
如何获得标题的完整列表以确保我获得了我应该获得的所有标题?
I have a simple mobile app in Titanium that I'm using to debug the ability to log into our user system.
At the moment, I cannot seem to see the Set-Cookie
response header as it's always returned as null
.
I'm currently using Titanium SDK 1.7.5 (1.8 is horribly broken).
My code is very simple, a text book example of using the 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.
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).
How can I get a full list of the headers to make sure I'm getting all the headers I'm supposed to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了我自己问题的答案。
我的代码中返回所有标头的内容是正确的。对于 iOS,使用
HTTPClient.getResponseHeaders()
是正确的方法,对于 Android,使用HTTPClient.getAllResponseHeaders()
是正确的方法(不知道为什么有两种不同的方法 - 这可能是另一个问题天)。我没有看到 cookie 标头的原因是 Titanium 1.7.5 中的一个错误(并且在 1.8.1 中仍然存在)。它不会在 302 重定向上转发 cookie。
Jiras 有关问题的信息:
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 andHTTPClient.getAllResponseHeaders()
for Android (no idea why there's two different ways - that could be a question for another day).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). It's not forwarding on the cookie on a 302 redirect.
Jiras on the issues: