Flash 套接字 HTTP-POST 示例

发布于 2024-12-07 11:20:39 字数 373 浏览 0 评论 0原文

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

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

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

我需要使用 HTTP-POST 请求。

额外奖励:这适用于 HTTPS 端口 443 吗?

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/flash/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?

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

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

发布评论

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

评论(1

梦与时光遇 2024-12-14 11:20:39

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

相反,请使用 URLRequest 类从 flash/actionscript 执行 HTTP 请求。该类支持 POST 和 GET。它还支持 HTTPS。

这里是执行 POST 请求的示例。 (顺便说一句,这是当您搜索 "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();
    }
}

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