在 Flex 中的 HTTP Post 中上传任意数据 [英] Upload arbitrary data in HTTP Post in Flex

查看:30
本文介绍了在 Flex 中的 HTTP Post 中上传任意数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前有一个 Java 小程序,可以生成大量数据并将其上传到我们的服务器.我们拥有的上传方法之一是使用 HTTP POST 到我们的服务器,其内容只是原始数据(没有多部分编码或任何东西).

We currently have a Java applet that generates a lot of data and uploads it to our servers. One of the upload methods we have is to use an HTTP POST to our server, the contents of which are just the raw data (no multipart encoding or anything).

我正在考虑实现一个执行相同操作的 Flex 应用程序,但我没有看到可以复制该网络行为的方法.Flex 中所有与 HTTP 相关的网络类似乎都在假设您只是发出请求的情况下工作.我没有看到实际设置 POST 有效负载的方法(除了设置几个查询参数,这在这里不起作用).

I'm looking at implementing a Flex app that does the same thing, but I don't see a way to duplicate that network behavior. All the HTTP-related network classes in Flex seem to work under the assumption that you're simply making a request. I don't see a way to actually set the payload of the POST (other than setting a couple query parameters, which wouldn't work here).

我真正想要的是能够启动 HTTP POST,然后能够像写入套接字一样写入连接.这是否可能而不必在 Socket 类之上编写我自己的 HTTP 实现?有没有办法在 Flex 中设置 HTTP POST 的负载?

What I'd really like is to be able to initiate an HTTP POST, but then be able to write to the connection like I would a socket. Is this possible without having to write my own HTTP implementation on top of the Socket class? Is there any way to set the payload of an HTTP POST within Flex?

澄清:能够创建数据缓冲区并将其发送到服务器就足够了(这看起来是可能的).如果我可以将连接视为套接字并随着时间的推移向它发送数据会更好,而不是一次发送所有数据,但这不是严格要求的.

Clarification: Being able to create a data buffer and send that to the server is sufficient (which it looks like is possible). It would be better if I could treat the connection as a socket and send data to it over time, instead of all at once, but that isn't strictly required.

推荐答案

这听起来像是在问两个问题.是的,有一种方法可以在 Flex 中设置 HTTP POST 的负载,并将任何格式的文件上传到您的服务器:

It sounds like you're sort of asking two questions. Yes, there is a way to set the payload of an HTTP POST within Flex, and to upload files of any format to your server:

private function doPost():void
{
    var yourData:ByteArray = new ByteArray();
    var encoder:Base64Encoder = new Base64Encoder();

    for (var i:int = 0; i < 10000; i++)
        yourData.writeByte(i);

    yourData.position = 0;
    encoder.encodeBytes(yourData);

    var req:URLRequest = new URLRequest("http://yourdomain.com/yourservice.ext");
    req.method = URLRequestMethod.POST;

    var postData:URLVariables = new URLVariables();
    postData.userData = encoder.flush();

    req.data = postData;

    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.BINARY;
    loader.addEventListener(Event.COMPLETE, loader_complete);
    loader.load(req);
}

private function loader_complete(event:Event):void
{
    trace("Upload complete!");
}

...但听起来你也想连接,保持连接打开,然后像套接字一样"任意写入;如果是这种情况,那么除了为此目的使用 Socket 类之外,我不相信有内置机制,不.尽管如此,希望它有所帮助!

... but it also sounds like you want to connect, keep the connection open and then write to it arbitrarily "like a socket"; if that's the case, then other than using the Socket classes for that purpose, I don't believe there's a built-in mechanism, no. Hope it helps nonetheless!

这篇关于在 Flex 中的 HTTP Post 中上传任意数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆