Flash Socket HTTP-POST示例

时间:2011-09-30 04:26:52

标签: actionscript-3 sockets https http-post

本文提供了使用flash.net.Socket类连接套接字的示例:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/Socket.html

底部的示例显示了如何使用HTTP-GET请求。

我需要使用HTTP-POST请求。

奖励:这是否适用于HTTPS端口443?

1 个答案:

答案 0 :(得分:2)

Socket不是这项工作的正确类。套接字用于处理原始TCP数据连接。例如,您将使用Socket与使用专有通信协议的自定义服务器组件集成。

而是使用URLRequest class从flash / actionscript执行HTTP请求。这个类支持POST和GET。它还支持HTTPS。

Here是执行POST请求的示例。 (顺便说一句,这是google在搜索"as3 post request"时)提供的第一个结果

文档中也有可用的示例(上面链接)以及网络上的其他地方。


编辑:要从HTTP服务器检索二进制流数据,使用URLStream。以下内容将通过POST请求完成此操作:

private var stream:URLStream;
private var uploadData:ByteArray;

public function URLStreamExample() {
    stream = new URLStream();
    stream.addEventListener(ProgressEvent.PROGRESS, progressHandler);

    var request:URLRequest = new URLRequest("URLStreamExample.swf");
    request.method = URLRequestMethod.POST;

    // uploadData contains the data to send with the post request
    // set the proper content type for the data you're sending
    request.contentType = "application/octet-stream";
    request.data = uploadData;  

    // initiate the request
    stream.load(request);
}

private function progressHandler(event:Event):void {
    // called repeatedly as data arrives (just like Socket's progress event)

    // URLStream is an IDataInput (just like Socket)
    while( stream.bytesAvailable ) {
       var b:int = stream.readByte();
    }
}