开发者

Flash Socket HTTP-POST Example

This article provides an example of using the flash.net.Socket class to connect to a socket:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fla开发者_如何学Pythonsh/net/Socket.html

The example at the bottom shows how to use an HTTP-GET request.

I need to use an HTTP-POST request.

Bonus: Does this work with HTTPS port 443?


The Socket is not the correct class for this job. Sockets are used for working with raw TCP data connections. For example, you would use Socket to integrate with custom server component that uses a proprietary communications protocol.

Instead, use the URLRequest class to perform HTTP requests from flash/actionscript. This class supports POST as well as GET. It also supports HTTPS.

Here is an example of doing a POST request. (incidentally, that is the first result google gives when you search for "as3 post request")

There are also examples available in the documentation (linked above), and other places around the net.


Edit: To retrieve binary streaming data from an HTTP server, you should use URLStream. Something like the following will accomplish this via a POST request:

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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜