When POSTing a form with URLRequest, how to include cookies from browser session?

做~自己de王妃 提交于 2019-12-04 10:38:07

Provided the cookie domains (of the page hosting the Flash control and the URL to which you're posting) match, then yes, the browser cookies get sent with the request by default. As a quick example (working version here), I've thrown together a simple ColdFusion page hosting a SWF containing the following code:

<mx:Script>
    <![CDATA[

        private function btn_click():void
        {
            var req:URLRequest = new URLRequest("http://test.enunciato.org/test.cfm");
            req.method = URLRequestMethod.POST;

            var postData:URLVariables = new URLVariables();
            postData.userName = "Joe";
            postData.userCoolness = "very-cool";

            req.data = postData;
            navigateToURL(req);
        }

    ]]>
</mx:Script>

<mx:Button click="btn_click()" label="Submit" />

... and in that page, I set a cookie, called "USERID", with a value of "12345". After clicking Submit, and navigating to another CFM, my server logs reveal the cookie passed through in the request:

POST /test.cfm HTTP/1.1 Mozilla/5.0
ASPSESSIONIDAASRDSRT=INDFAPMDINJLOOAHDELDNKBL;
JSESSIONID=60302395a68e3d3681c2;
USERID=12345
test.enunciato.org 200

If you test it out yourself, you'll see the postData in there as well.

Make sense?

I'm assuming you just wan't to include something like a session id for authentication purposes server-side.

To get the browser cookie from AS (needs javascript enabled, shouldn't be a problem for most users)

public var cookieStr:String;
public var cookieHeader:URLRequestHeader;
ExternalInterface.call('eval','window.cookieStr = function () {return  document.cookie};')
cookieStr = ExternalInterface.call('cookieStr'); 
cookieHeader = new URLRequestHeader("Cookie",cookieStr);

Then, when using your URLRequest object:

var urlRequest:URLRequest = new URLRequest(...blah blah, url here, etc etc);
urlRequest.requestHeaders.push(cookieHeader);

Take note that Firefox does not send the cookies of the session along with a URLRequest, you will need a solution similar to the one above to overcome this problem.

Not sure about flash. But couldn't you serialize the cookie and put it into the URL?

Maybe you would want to encrypt the data or transmit it in plain text but it might look like:

url:

www.example.com?newurl&cookiesession=true&cookieusername=bob

etc

(or I am missing something?)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!