用自定义标题发送POST请求 [英] Send POST request with custom headers using flash

查看:167
本文介绍了用自定义标题发送POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用自定义标题发送POST请求使用Flash,但我只能让它发送请求作为GET,即使当我使用 request.method = URLRequestMethod.POST; $ b $ p
$ b $ p $ {code $ package $ {

$ b import flash.display.Sprite;
导入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 header:Array = [
new URLRequestHeader(X-CUSTOM-HEADER,X-VALUE),
new URLRequestHeader(Accept,application / json)
];
var requestVars:URLVariables = new URLVariables();
requestVars.test =test;
request.data = requestVars; //如果这条线和它下面的那条线被注释掉了,那么这个请求就是GET
request.requestHeaders = headers; // ^没有请求标头
var request:URLRequest = new URLRequest();
request.method = URLRequestMethod.POST; //使用URLRequestMethod.POST,但请求仍然以GET
request.url = url的形式发送。
尝试{
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);

$ b 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);





$ p $并且在我的crossdomain.xml我有:

 <?xml version =1.0?> 
<!DOCTYPE跨域策略系统http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">
<跨网域政策>
< allow-http-request-headers-from domain =*headers =*secure =false/>
< / cross-domain-policy>


解决方案

强大的>有效的附加的POST数据。只要我至少向URLVariables对象添加了一个有效的键值对,我的Chrome开发工具就会将请求方法报告为POST: http://wonderfl.net/c/Ia2z


I am trying to send POST request with custom headers using flash, but I can only get it to send the request as GET even when I use request.method = URLRequestMethod.POST;

Here is my code:

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

And in my crossdomain.xml file I have:

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

解决方案

It seems it handles request as GET if there's no valid POST data attached. As soon as I added at least one valid key:value pair to the URLVariables object my Chrome dev tool reported the request method as POST: http://wonderfl.net/c/Ia2z

这篇关于用自定义标题发送POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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