How to access all HTTP response headers

I have a simple mobile application in Titanium that I use to debug the ability to log into our user system.

At the moment, I cannot see the header of the Set-Cookie response, as it always returned as null .

I am currently using the Titanium SDK 1.7.5 (1.8 is terribly broken).

My code is very simple, an example text book using 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’ll just go around and around, because, it seems, I don’t see what is wrong here. What confuses me is that HTTPClient.getResponseHeaders() is not even documented ( Titanium.Network.HTTPClient-object.html ) - and does not work for Android.

I know that there must be something, because the webview displays the authenticated page in order (you cannot get there if you are not authorized + cookie).

How can I get a complete list of headers to make sure that I get all the headers that I should use?

+4
source share
1 answer

I found the answer to my 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 (I don’t know why there are two different ways - it could be a question for another day).

The reason I don't see the cookie header is due to a bug in titanium 1.7.5 (and still exists in 1.8.1). This is not a 302 redirect to a cookie.

Jiras on:

+2
source

Source: https://habr.com/ru/post/1394315/


All Articles