ITeye博客

JSON-RPC、XML-RPC、SOAP三者的关系(转)

sangei · 2014-02-22 21:58 · SOA · 编程语言 · Markdown

JSON-RPC规范:http://json-rpc.org/wiki/specification

XML-RPC规范:http://www.xmlrpc.com/spec

SOAP规范:http://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383487

参考:http://weblog.masukomi.org/writings/xml-rpc_vs_soap.htm

三者都是为了实现远程过程调用RPC中的消息交换,并且都没有定义传输协议。不过为了更方便在网络中传输,而且由于HTTP的无状态性,都使得HTTP为这三者的常用的传输协议。下面例子也是基于HTTP协议的

XML-RPC和SOAP都是基于XML格式的消息交换:

XML-RPC非常简单,定义了几种基本类型、匿名结构体、匿名数组;

SOAP除了基本类型、命名结构体、命名数组以外,还可以自定义类型,能使用多态的方法调用方式

而JSON-RPC是基于JSON格式的消息交换,JSON比XML更加轻巧,并且非常容易在页面JS中使用,其他特点与XML-RPC类似

下面是使用这几种协议发送请求的例子:

XML-RPC

Xhtml代码

  1. POST /RPC2 HTTP/1.0
  2. User-Agent: Frontier/5.1.2 (WinNT)
  3. Host: betty.userland.com
  4. Content-Type: text/xml
  5. Content-length: 181
  6. <?xml version="1.0"?>
  7. <methodCall>
  8. <methodName>examples.getStateName</methodName>
  9. <params>
  10. <param>
  11. <value><i4>41</i4></value>
  12. </param>
  13. </params>
  14. </methodCall>

SOAP:

Xhtml代码

  1. POST /StockQuote HTTP/1.1
  2. Host: www.stockquoteserver.com
  3. Content-Type: text/xml; charset="utf-8"
  4. Content-Length: nnnn
  5. SOAPAction: "Some-URI"
  6. <SOAP-ENV:Envelope
  7. xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  8. SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  9. <SOAP-ENV:Header>
  10. <t:Transaction
  11. xmlns:t="some-URI"
  12. SOAP-ENV:mustUnderstand="1">
  13. 5
  14. </t:Transaction>
  15. </SOAP-ENV:Header>
  16. <SOAP-ENV:Body>
  17. <m:GetLastTradePrice xmlns:m="Some-URI">
  18. <symbol>DEF</symbol>
  19. </m:GetLastTradePrice>
  20. </SOAP-ENV:Body>
  21. </SOAP-ENV:Envelope>

JSON:

Javascript代码

  1. --> { "method": "echo", "params": ["Hello JSON-RPC"], "id": 1}
  2. <-- { "result": "Hello JSON-RPC", "error": null, "id": 1}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。