2012-02-02 34 views
4

我有一個簡單的移動應用程序,我使用調試登錄到我們的用戶系統的能力。如何訪問鈦所有的HTTP響應頭

目前,我似乎無法看到Set-Cookie響應標題,因爲它始終返回爲null。目前我使用的是鈦合金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不起作用。

我知道一定有什麼東西出現,因爲網頁視圖顯示認證頁面罰款(你無法到達那裏,除非你授權+餅乾)。

我怎樣才能得到頭的完整列表,以確保我得到我應該在所有的標題?

回答

2

我已經找到了答案,以我自己的問題。

我在我的代碼是什麼返回所有的頭是正確的。使用HTTPClient.getResponseHeaders()是iOS和正確的方法HTTPClient.getAllResponseHeaders()爲Android(不知道爲什麼有兩種不同的方式 - 這可能是另一天的一個問題)。

的原因,我沒有看到小甜餅報頭是因爲鈦1.7.5中的錯誤(在1.8.1仍然存在)。它不轉發對302重定向的餅乾。

Jiras上的問題:

+1

謝謝。這麼多。我正要扔我的筆記本電腦窗外,但是這是一個更好的解決方案。 – pancake 2012-07-11 13:42:57