将curl转换为flex

时间:2009-12-09 11:13:59

标签: flex curl

任何人都可以告诉我在flex中的相应内容

curl --insecure --request POST --basic -u testuser@mydomain.com:password --header“Content-Type:application / xml”--data-binary“@c:\ curl \ examples \ New_Activity.xml“https://beta.12sprints.com/v1/activities

基本上这是一个api我需要发送用户凭据和包含数据的xml文件(new_activity)

对于凭据我尝试将其添加为标头验证并将其编码为base64

var enc:Base64Encoder = new Base64Encoder(); enc.encode(“saurav.das@sap.com”+“:”+密码); myservice.headers [“Authorization”] =“basic”+ enc.toString(); myservice.send();

但这也不起作用...... 请帮忙..

2 个答案:

答案 0 :(得分:1)

just_a_dude具有身份验证部分,但他的示例中的xml不适用于当前的12sprints API,并且它不应该是Base64编码的。这是他的样本的修改版本(只需更改用户名/密码):

            // the xml we want to send to the server
            var xml:String = "<activity name=\"New activity using cURL\"></activity>"


            var bytes:ByteArray = new ByteArray();
            bytes.writeUTFBytes(xml);

            // encoded credentials
            var credentials:Base64Encoder = new Base64Encoder();
            credentials.encode("testuser@example.com:pass");


            var request:URLRequest = new URLRequest("https://beta.12sprints.com/v1/activities");
            request.data = bytes;
            request.method = URLRequestMethod.POST;
            request.requestHeaders.push(new URLRequestHeader("Authorization", "Basic " + credentials));
            request.requestHeaders.push(new URLRequestHeader("Content-Type", "application/xml"));

            var loader:URLLoader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.BINARY;

            loader.addEventListener(Event.COMPLETE, completeHandler);
            loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);

            loader.load(request);   
    }

    protected function completeHandler(event:Event):void {
            trace("complete");
    }

    protected function errorHandler(event:Event):void {
            trace("error : ", event);

            var loader:URLLoader = event.currentTarget as URLLoader;
            trace(loader.data); 
    }

答案 1 :(得分:0)

我不确定,但也许这是要走的路

package {

import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.utils.ByteArray;

import mx.graphics.codec.PNGEncoder;
import mx.utils.Base64Encoder;

[SWF(backgroundColor = "0xffffff", width = 500, height = 400)]

public class UploadExample extends Sprite {

    public function UploadExample () {

        // the xml we want to send to the server
        var xml:XML = <data>
            <activity>
                <title>foo</title>
                <description>foo bar rules</description>
                <created>2009-12-09 15:14:00</created>
            </activity>
        </data>;


        var bytes:ByteArray = new ByteArray();
        bytes.writeUTFBytes(xml);

        // encoded data
        var data:Base64Encoder = new Base64Encoder();
        data.encodeBytes(bytes);

        // encoded credentials
        var credentials:Base64Encoder = new Base64Encoder();
        credentials.encode("testuser@mydomain.com:password");


        var request:URLRequest = new URLRequest("https://beta.12sprints.com/v1/activities");
        request.data = data;
        request.method = URLRequestMethod.POST;
        request.requestHeaders.push(new URLRequestHeader("Authorization", "Basic " + credentials));
        request.requestHeaders.push(new URLRequestHeader("Content-Type", "application/xml"));

        var loader:URLLoader = new URLLoader();
        loader.dataFormat = URLLoaderDataFormat.BINARY;

        loader.addEventListener(Event.COMPLETE, completeHandler);
        loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);

        loader.load(request);   
    }

    protected function completeHandler(event:Event):void {
        trace("complete");
    }

    protected function errorHandler(event:Event):void {
        trace("error : ", event);

        var loader:URLLoader = event.currentTarget as URLLoader;
        trace(loader.data); 
        /* this is what get ...
        <?xml version="1.0" encoding="UTF-8"?>
        <error>
          <http_status>401 Unauthorized</http_status>
          <message>Could not authenticate you.</message>
        </error>
        */
    }


}
}