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

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

问题描述

我们现在有一个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,但能写出像我会一个套接字连接。这是可能的,而无需编写自己的HTTP实现对Socket类的顶部?有没有什么办法可以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!

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

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