使用flash发送带有自定义标头的POST请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用flash发送带有自定义标头的POST请求相关的知识,希望对你有一定的参考价值。

我正在尝试使用flash发送带有自定义标头的POST请求,但即使我使用request.method = URLRequestMethod.POST;,我也只能将它发送为GET请求

这是我的代码:

package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.*;

public class URLRequestExample extends Sprite {
    private var loader:URLLoader;

    public function URLRequestExample() {
        loader = new URLLoader();
        configureListeners(loader);
        var url:String = "http://example.com/";
        var headers:Array = [
            new URLRequestHeader("X-CUSTOM-HEADER", "X-VALUE"),
            new URLRequestHeader("Accept", "application/json")
        ];
        var requestVars:URLVariables = new URLVariables();
        requestVars.test = "test";
        request.data = requestVars; // If this line and the one under it are commented the request goes as GET
        request.requestHeaders = headers; // ^with no request headers
        var request:URLRequest = new URLRequest();
        request.method = URLRequestMethod.POST; // Using URLRequestMethod.POST but the request is still sent as GET
        request.url = url;
        try {
            loader.load(request);
        } catch (error:Error) {
            trace("Unable to load requested document.");
        }
    }

    private function configureListeners(dispatcher:IEventDispatcher):void {
        dispatcher.addEventListener(Event.COMPLETE, completeHandler);
        dispatcher.addEventListener(Event.OPEN, openHandler);
        dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
        dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    }

    private function completeHandler(event:Event):void {
        var loader:URLLoader = URLLoader(event.target);
        trace("completeHandler: " + loader.data);
    }

    private function openHandler(event:Event):void {
        trace("openHandler: " + event);
    }

    private function progressHandler(event:ProgressEvent):void {
        trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }

    private function httpStatusHandler(event:HTTPStatusEvent):void {
        trace("httpStatusHandler: " + event);
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event);
    }
}
}

在我的crossdomain.xml文件中,我有:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
    <site-control permitted-cross-domain-policies="all"/>
    <allow-access-from domain="*" secure="false"/>
    <allow-http-request-headers-from domain="*" headers="*" secure="false"/>
</cross-domain-policy>
答案

如果没有附加有效的POST数据,它似乎将请求作为GET处理。只要我将至少一个有效的键:值对添加到URLVariables对象,我的Chrome开发工具就会将请求方法报告为POST:http://wonderfl.net/c/Ia2z

以上是关于使用flash发送带有自定义标头的POST请求的主要内容,如果未能解决你的问题,请参考以下文章

发送带有自定义 HTTP 标头的跨域请求时禁用预检 OPTION 请求

如何在scrapy中发送带有标头和有效负载的Post请求

带有自定义标头的 Ajax 请求发送到启用 CORS 的 Web API 服务器

如何在 Swagger UI 中发送带有请求的自定义标头?

带有自定义标头的 Express 正文解析器

如何阻止用户使用 JWT 创建自定义 POST 请求?