Welcome, Guest. Please login or register.
Did you miss your activation email?
31 Jul 2010, 11:35:52 UTC
Forum home
+  flexdeveloper.eu Forum
|-+  AIR
| |-+  AIR (Moderators: JMWhittaker, Jan K, thewarpedcoder, James)
| | |-+  Requestheaders are empty?
« previous next »
Pages: [1] Print
Author Topic: Requestheaders are empty? (Read 870 times)
mauri824
Newbie FD
*
Posts: 5


« on: 11 Jan 2010, 04:29:40 UTC »

Hey guys, I have a few questions which shouldn't be too complicated if you know air/actionscript


Alright, so I've got a bit of a problem here.  What I want my code to do is to login you to a 3rd party site, and to do that it has to be able to send/receive cookies.

1) Shouldn't request.manageCookies = true; take care of this?
2) assuming it doesn't, or if I just wanted to do it the hard way, I'm guessing I could set cookies by checking the response headers, but my response headers are always EMPTY.  my code is below and as you can probably tell I copy+pasted a print_r function to print the headers (i also tried other functions, but they all showed the array being empty)


Maybe I missed something easy, I have no idea. Thanks in advance!


Code:

function print_r (array, return_val) {
    // http://kevin.vanzonneveld.net
    // +   original by: Michael White (http://getsprink.com)
    // +   improved by: Ben Bryan
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +      improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: echo
    // *     example 1: print_r(1, true);
    // *     returns 1: 1
   
    var output = "", pad_char = " ", pad_val = 4, d = this.window.document;
    var getFuncName = function (fn) {
        var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
        if (!name) {
            return '(Anonymous)';
        }
        return name[1];
    };

    var repeat_char = function (len, pad_char) {
        var str = "";
        for (var i=0; i < len; i++) {
            str += pad_char;
        }
        return str;
    };

    var formatArray = function (obj, cur_depth, pad_val, pad_char) {
        if (cur_depth > 0) {
            cur_depth++;
        }

        var base_pad = repeat_char(pad_val*cur_depth, pad_char);
        var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char);
        var str = "";

        if (typeof obj === 'object' && obj !== null && obj.constructor && getFuncName(obj.constructor) !== 'PHPJS_Resource') {
            str += "Array\n" + base_pad + "(\n";
            for (var key in obj) {
                if (obj[key] instanceof Array) {
                    str += thick_pad + "["+key+"] => "+formatArray(obj[key], cur_depth+1, pad_val, pad_char);
                } else {
                    str += thick_pad + "["+key+"] => " + obj[key] + "\n";
                }
            }
            str += base_pad + ")\n";
        } else if (obj === null || obj === undefined) {
            str = '';
        } else { // for our "resource" class
            str = obj.toString();
        }

        return str;
    };

    output = formatArray(array, 0, pad_val, pad_char);

    if (return_val !== true) {
        if (d.body) {
            this.echo(output);
        }
        else {
            try {
                d = XULDocument; // We're in XUL, so appending as plain text won't work; trigger an error out of XUL
                this.echo('<pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">'+output+'</pre>');
            }
            catch (e) {
                this.echo(output); // Outputting as plain text may work in some plain XML
            }
        }
        return true;
    } else {
        return output;
    }
}


function login() {
var errors;


  var request = new air.URLRequest(); 
  request.url = "post.php"; 
  request.method = air.URLRequestMethod.POST; 
  request.requestHeaders = new Array();
  request.manageCookies = true;
  request.followRedirects = true; 
  request.contentType = "text/html";
  var loader = new air.URLLoader();
  loader.dataFormat = air.URLLoaderDataFormat.VARIABLES;
  var agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008100922 Ubuntu/8.04 (hardy) Firefox/3.0.3";
  request.userAgent =  agent;
  var username = 'email@gmail.com';
  var pass = 'pass';
  var dataXML = "charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&email=" + username + "&pass=" + pass + "&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&version=0.2.3&md5pass=1&noerror=1&challenge=9a8c91fcad71c4cfc6d195ba04d39a56";
  var variables = new air.URLVariables(dataXML);
  request.data = variables;
  loader.dataFormat = air.URLLoaderDataFormat.TEXT; 
  loader.addEventListener(air.Event.COMPLETE, completeHandler); 
 
  try 
  { 

  loader.addEventListener(air.HTTPStatusEvent.HTTP_RESPONSE_STATUS, responseStatusHandler);
//loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, statusHandler);


  loader.load(request); 
 
  } 
  catch (error) 
  { 
  air.trace("Unable to load URL"); 
 
  } 

function responseStatusHandler(event) {
var test = print_r(event.responseHeaders, true);
alert(test);
}

function statusHandler(event) {
}

function completeHandler(event)  { 
 var loader = air.URLLoader(event.target);
 var xml = loader.data;
 xml = unescape(xml);
 document.getElementById("continue4").innerHTML = xml; 
 responseHeaders = event["responseHeaders"];
  }

if(document.loginform.email.value.length < 1) {
alert("You must enter in an email");
errors = 1;
}

if(document.loginform.pass.value.length < 1) {
alert("You must enter in a password");
errors = 1;
}

if(!errors) {

mhide("myBox");
mshow("continue");
}

else {

}
}

Logged
flexy
flexdeveloper.eu
Guru/Addict FD
*****
Posts: 3,155


Recovering Coffee Addict & Adobe Expert


WWW
« Reply #1 on: 11 Jan 2010, 10:21:28 UTC »

You can't interrogate response headers in Flash Player at this time: https://bugs.adobe.com/jira/browse/FP-251
Logged

mauri824
Newbie FD
*
Posts: 5


« Reply #2 on: 11 Jan 2010, 14:49:48 UTC »

You can't interrogate response headers in Flash Player at this time: https://bugs.adobe.com/jira/browse/FP-251
Thanks, at least I can quit trying that now  Tongue

But, going back to my original problem, is there any possible way to receive cookies when using urlrequest?
Logged
flexy
flexdeveloper.eu
Guru/Addict FD
*****
Posts: 3,155


Recovering Coffee Addict & Adobe Expert


WWW
« Reply #3 on: 11 Jan 2010, 15:50:11 UTC »

Flash Player has no internal way of querying browser or session cookies. You can access browser cookies using ExternalInterface (to call a JavaScript method in the parent web page), but session cookies cannot be read by your app.
Logged

mauri824
Newbie FD
*
Posts: 5


« Reply #4 on: 11 Jan 2010, 16:33:21 UTC »

Flash Player has no internal way of querying browser or session cookies. You can access browser cookies using ExternalInterface (to call a JavaScript method in the parent web page), but session cookies cannot be read by your app.

Alright, two more questions and then I won't bug anymore  Tongue

I'm not using flash, I'm making an ajax/html air app, is it still the same way? I was assuming it would be but it doesn't hurt to ask

and this is more of a general js question than an air question, what about using xmlhttprequest? that should let me read the response headers, right?
Logged
flexy
flexdeveloper.eu
Guru/Addict FD
*****
Posts: 3,155


Recovering Coffee Addict & Adobe Expert


WWW
« Reply #5 on: 11 Jan 2010, 17:03:42 UTC »

You'd be writing your app with JavaScript, and then the supported features of the WebKit implementation in AIR is your limitation.
Logged

mauri824
Newbie FD
*
Posts: 5


« Reply #6 on: 21 Jan 2010, 16:53:46 UTC »

First things first, I realized the title was wrong, meant to say responseheaders not requestheaders, but I think you understood that anyhow  Smiley


Ok, so I was working on my code again and the headers are being set,
If i write alert(event.responseHeaders); I get a message saying [Object URLRequestHeader], [Object URLRequestHeader], and so on

The headers are being set since this is an air app, I just now don't know where to go from here and getting the headers as text. I was assuming the print_r function would work but it won't because responseheaders is an array of objects.   How would I code this with javascript Huh

First I gotta loop through the array with this

alert(event.responseHeaders);


but that just gives me [Object URLRequestHeader] and I don't know where to go from there
Logged
flexy
flexdeveloper.eu
Guru/Addict FD
*****
Posts: 3,155


Recovering Coffee Addict & Adobe Expert


WWW
« Reply #7 on: 21 Jan 2010, 17:09:50 UTC »

http://livedocs.adobe.com/flex/3/langref/flash/net/URLRequestHeader.html
Logged

mauri824
Newbie FD
*
Posts: 5


« Reply #8 on: 21 Jan 2010, 18:02:35 UTC »


I spent all night looking at that page but didn't know how to code this.

I was going to ask again but I finallly figured it out  Smiley

I just didn't know how to code this because I'm not very big on ajax or actionscript, sorry.

alert(event.responseHeaders.name);
alert(event.responseHeaders.value);

was what I was after  Embarrassed
Logged
flexy
flexdeveloper.eu
Guru/Addict FD
*****
Posts: 3,155


Recovering Coffee Addict & Adobe Expert


WWW
« Reply #9 on: 22 Jan 2010, 09:00:29 UTC »

Essentially it looks like you're getting an Array of ResponseHeader object back, so to interrogate each one individually, you need to loop:

Please excuse my JavaScript:

Code:
var headers = event.responseHeaders;
var len = headers.length;
for( var i=0; i<len; i++ )
{
    alert( headers[ i ].name + " : " + headers[ i ].value );
}
Logged

Pages: [1] Print
« previous next »
Share this on: Twitter Twitter Del.icio.us del.icio.us Digg Digg
Jump to:

©2006-2010 Flexdeveloper.eu/Jodie O'Rourke. All rights reserved.
Adobe®, Adobe® Flash™, Adobe® AIR™ and Adobe® Flex™ are registered trademarks of Adobe Systems Incorporated in the United States and other countries. All rights reserved.

Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC