XML-RPC Web 服务请求

Flash Player 9 和更高版本,Adobe AIR 1.0 和更高版本

XML-RPC Web 服务将其调用参数看作 XML 文档而不是一组 URL 变量。要使用 XML-RPC Web 服务执行事务,请创建格式正确的 XML 消息并使用 HTTP POST 方法将其发送到 Web 服务。此外,您应该为请求设置 Content-Type 标题,以便服务器将请求数据看作 XML。

下面的示例演示如何使用与 REST 示例中显示的相同的 Web 服务调用,但这次是 XML-RPC 服务:

import flash.events.Event; 
import flash.events.ErrorEvent; 
import flash.events.IOErrorEvent; 
import flash.events.SecurityErrorEvent; 
import flash.net.URLLoader; 
import flash.net.URLRequest; 
import flash.net.URLRequestMethod; 
import flash.net.URLVariables; 
public function xmlRPCRequest():void 
{ 
    //Create the XML-RPC document 
    var xmlRPC:XML = <methodCall> 
                                    <methodName></methodName> 
                                    <params> 
                                        <param> 
                                            <value> 
                                                <struct/> 
                                            </value> 
                                        </param> 
                                    </params> 
                                </methodCall>; 
     
    xmlRPC.methodName = "test.echo"; 
     
    //Add the method parameters 
    var parameters:Object = new Object(); 
    parameters.api_key = "123456ABC"; 
    parameters.message = "Able was I, ere I saw Elba."; 
     
    for( var propertyName:String in parameters ) 
    { 
        xmlRPC..struct.member[xmlRPC..struct.member.length + 1] = 
            <member> 
                <name>{propertyName}</name> 
                <value> 
                    <string>{parameters[propertyName]}</string> 
                </value> 
            </member>; 
    } 
     
    //Create the HTTP request object 
    var request:URLRequest = new URLRequest( "http://service.example.com/xml-rpc/" ); 
    request.method = URLRequestMethod.POST; 
    request.cacheResponse = false; 
    request.requestHeaders.push(new URLRequestHeader("Content-Type", "application/xml")); 
    request.data = xmlRPC; 
     
    //Initiate the request 
    requestor = new URLLoader(); 
    requestor.dataFormat = URLLoaderDataFormat.TEXT; 
    requestor.addEventListener( Event.COMPLETE, xmlRPCRequestComplete ); 
    requestor.addEventListener( IOErrorEvent.IO_ERROR, xmlRPCRequestError ); 
    requestor.addEventListener( SecurityErrorEvent.SECURITY_ERROR, xmlRPCRequestError ); 
    requestor.load( request ); 
} 
 
private function xmlRPCRequestComplete( event:Event ):void 
{ 
    trace( XML(event.target.data).toXMLString() );     
} 
 
private function xmlRPCRequestError( error:ErrorEvent ):void 
{ 
    trace( "An error occurred: " + error );     
}

AIR 中的 WebKit 不支持 E4X 语法,因此在上一示例中用于创建 XML 文档的方法在 JavaScript 代码中不可用。您必须使用 DOM 方法创建 XML 文档或将文档创建为字符串,并使用 JavaScript DOMParser 类将字符串转换为 XML。

在以下示例中,使用 DOM 方法创建 XML-RPC 消息和 XMLHttpRequest 来执行 Web 服务事务:

<html> 
<head> 
<title>XML-RPC web service request</title> 
<script type="text/javascript"> 
 
function makeRequest() 
{ 
    var requestDisplay = document.getElementById( "request" ); 
    var resultDisplay  = document.getElementById( "result" ); 
     
    var request = {}; 
    request.URL = "http://services.example.com/xmlrpc/"; 
    request.method = "test.echo"; 
    request.HTTPmethod = "POST"; 
    request.parameters = {}; 
    request.parameters.api_key = "123456ABC"; 
    request.parameters.message = "Able was I ere I saw Elba."; 
    var requestMessage = formatXMLRPC( request ); 
     
    xmlhttp = new XMLHttpRequest(); 
    xmlhttp.open( request.HTTPmethod, request.URL, true); 
    xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState == 4) { 
            resultDisplay.innerText = xmlhttp.responseText; 
        } 
    } 
    xmlhttp.send( requestMessage ); 
     
    requestDisplay.innerText = xmlToString( requestMessage.documentElement ); 
} 
 
//Formats a request as XML-RPC document 
function formatXMLRPC( request ) 
{ 
    var xmldoc = document.implementation.createDocument( "", "", null ); 
    var root = xmldoc.createElement( "methodCall" ); 
    xmldoc.appendChild( root ); 
    var methodName = xmldoc.createElement( "methodName" ); 
    var methodString = xmldoc.createTextNode( request.method ); 
    methodName.appendChild( methodString ); 
     
    root.appendChild( methodName ); 
     
        var params = xmldoc.createElement( "params" ); 
        root.appendChild( params ); 
         
        var param = xmldoc.createElement( "param" ); 
        params.appendChild( param ); 
        var value = xmldoc.createElement( "value" ); 
        param.appendChild( value ); 
        var struct = xmldoc.createElement( "struct" ); 
        value.appendChild( struct ); 
         
        for( var property in request.parameters ) 
        { 
            var member = xmldoc.createElement( "member" ); 
            struct.appendChild( member ); 
             
            var name = xmldoc.createElement( "name" ); 
            var paramName = xmldoc.createTextNode( property ); 
            name.appendChild( paramName ) 
            member.appendChild( name ); 
             
            var value = xmldoc.createElement( "value" ); 
            var type = xmldoc.createElement( "string" ); 
            value.appendChild( type ); 
            var paramValue = xmldoc.createTextNode( request.parameters[property] ); 
            type.appendChild( paramValue ) 
            member.appendChild( value ); 
        } 
    return xmldoc; 
} 
 
//Returns a string representation of an XML node 
function xmlToString( rootNode, indent ) 
{ 
    if( indent == null ) indent = ""; 
    var result = indent + "<" + rootNode.tagName + ">\n"; 
    for( var i = 0; i < rootNode.childNodes.length; i++) 
    { 
        if(rootNode.childNodes.item( i ).nodeType == Node.TEXT_NODE ) 
        { 
            result += indent + "    " + rootNode.childNodes.item( i ).textContent + "\n"; 
        } 
    } 
    if( rootNode.childElementCount > 0 ) 
    { 
        result += xmlToString( rootNode.firstElementChild, indent + "    " ); 
    } 
    if( rootNode.nextElementSibling ) 
    { 
        result += indent + "</" + rootNode.tagName + ">\n"; 
        result += xmlToString( rootNode.nextElementSibling, indent ); 
    } 
    else 
    { 
        result += indent +"</" + rootNode.tagName + ">\n"; 
    } 
    return result; 
} 
 
</script> 
</head> 
<body onload="makeRequest()"> 
<h1>Request:</h1> 
<pre id="request"></pre> 
<h1>Result:</h1> 
<pre id="result"></pre> 
</body> 
</html>