使用 URLRequest 发布表单时,如何包含浏览器会话中的 cookie?

发布于 2024-07-13 02:57:13 字数 271 浏览 8 评论 0原文

(参考这个答案:)

当我使用 URLRequest 进行 POST 时,它是否会自动包含来自托管 Flash 的浏览器会话的 cookie? 如果没有,我怎样才能让它包含它们,或者如果有必要的话,我自己检索它们并包含它们?

(With reference to this answer:)

When I POST with a URLRequest, does it automatically include cookies from the browser session in which Flash is hosted? If not, how can I make it include them, or if necessary retrieve them and include them myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南巷近海 2024-07-20 02:57:13

如果 cookie 域(托管 Flash 控件的页面和您要发布到的 URL)匹配,那么默认情况下,浏览器 cookie 会随请求一起发送。 作为一个简单的示例(此处的工作版本),我整理了一个简单的 ColdFusion 页面,其中托管SWF 包含以下代码:

<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" />

...在该页面中,我设置了一个名为“USERID”的 cookie,其值为“12345”。 单击“提交”并导航到另一个 CFM 后,我的服务器日志显示请求中传递的 cookie:

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

如果您自己测试一下,您也会在那里看到 postData。

合理?

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?

猫弦 2024-07-20 02:57:13

我假设您只是不想包含用于服务器端身份验证目的的会话 ID 之类的内容。

从 AS 获取浏览器 cookie(需要启用 JavaScript,对于大多数用户来说应该不是问题)

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);

然后,在使用 URLRequest 对象时:

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

请注意 Firefox 不会发送会话的 cookie使用 URLRequest,您将需要一个与上面类似的解决方案来解决这个问题。

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.

感情洁癖 2024-07-20 02:57:13

不确定闪光灯。 但是你不能序列化cookie并将其放入URL中吗?

也许您想加密数据或以纯文本形式传输数据,但它可能看起来像:

url:

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

(或者我遗漏了一些东西?)

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?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文