如何使用XmlRpc-Client自定义xml请求

时间:2013-03-25 19:35:10

标签: xmlrpcclient

我遇到了这个问题:我正在尝试使用Apache的XmlRpc-Client发送自定义xml请求。我有一个XmlRpcClient的实例,它包含一些名为“execute”的方法,如下所示:

public class RPCClient {
    public static void main(String[] args) {
        try {
            XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
            config.setServerURL(new URL("http://localhost:8888/SOAP"));

            XmlRpcClient client = new XmlRpcClient();
            client.setConfig(config);

            Object[] params = new Object[]{new Integer(4), new Integer(3)};
            String myResponse = (String) client.execute("A_Method", params);

            System.out.println("Suc " + myResponse);

        } catch (Exception e) {
            System.out.println("Err " + e.getMessage());
        }
    }
}

它正在发送:

POST /SOAP HTTP/1.1
Content-Type: text/xml
User-Agent: Apache XML RPC 3.1.3 (Sun HTTP Transport)
Cache-Control: no-cache
Pragma: no-cache
Host: ***********
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 200

<?xml version="1.0" encoding="UTF-8"?>
   <methodCall>
      <methodName>A_Method</methodName>
      <params>
         <param>
            <value>
               <i4>4</i4>
            </value>
         </param>
         <param>
            <value>3</value>
         </param>
      </params>
   </methodCall>

简而言之,这个客户端没有“执行”方法来接收简单的字符串或类似的东西。所以我的问题是如何发送我的自定义xml,它应该是这样的:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:rp="http://www.abcdef.com/GH"xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">   
   <soapenv:Header>   </soapenv:Header>   
   <soapenv:Body>      
      <rp:A_Methodsoapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">         
         <package xsi:type="xsd:string">string1</package>         
         <event xsi:type="xsd:string">string2</event>         
         <fields xsi:type="rp:ArrayOfKeyValuePair" soapenc:arrayType="rp:KeyValuePair[]">        
            <item xsi:type="rp:KeyValuePair">                       
               <key xsi:type="xsd:string">string3</key>                         
               <value xsi:type="xsd:string">123</value>        
            </item>        
         </fields>      
      </rp:A_Method>   
   </soapenv:Body></soapenv:Envelope>

提前感谢!

1 个答案:

答案 0 :(得分:0)

Apache XML-RPC用于与XML-RPC协议进行通信。您尝试发送的邮件是SOAP,这是一种不同的协议(请参阅问题"What's the difference between XML-RPC and SOAP?")。

要发送此消息,您可以使用常规HTTP客户端以纯文本(如果它只是一条简单消息)发送它,也可以使用完整的SOAP库。