现在的位置: 首页 > 综合 > 正文

JSON-RPC、XML-RPC、SOAP三者的关系

2013年03月05日 ⁄ 综合 ⁄ 共 3266字 ⁄ 字号 评论关闭

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.   
  7.   
  8.   
  9. <?xml version="1.0"?>  
  10. <methodCall>  
  11.    <methodName>examples.getStateName</methodName>  
  12.    <params>  
  13.       <param>  
  14.          <value><i4>41</i4></value>  
  15.          </param>  
  16.       </params>  
  17.    </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.   
  7. <SOAP-ENV:Envelope  
  8.   xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"  
  9.   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>  
  10.    <SOAP-ENV:Header>  
  11.        <t:Transaction  
  12.            xmlns:t="some-URI"  
  13.            SOAP-ENV:mustUnderstand="1">  
  14.                5  
  15.        </t:Transaction>  
  16.    </SOAP-ENV:Header>  
  17.    <SOAP-ENV:Body>  
  18.        <m:GetLastTradePrice xmlns:m="Some-URI">  
  19.            <symbol>DEF</symbol>  
  20.        </m:GetLastTradePrice>  
  21.    </SOAP-ENV:Body>  
  22. </SOAP-ENV:Envelope>  

JSON:

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

转正 http://kingquake21.iteye.com/blog/1033471

REST以原始的http协议来传送信息。它属于RESTfull的范畴。
以GET或POST传送(废话),适用于传送简单的request信息。
以XML形式返回(response)。
开发时服务器端可以和一般的Web网站一样构建,客户端只要有解析HTTP和XML的DOM API即可。
采用RESTfull最有名的应该算是Twitter.com。
REST开发起来比较容易。

request

GET /WebSite1/WebService.asmx/getHello?str=string HTTP/1.1

Host: localhost

response

HTTP/1.1 200 OK

Content-Type: text/xml; charset=utf-8

Content-Length: length

 

<?xml version="1.0" encoding="utf-8"?>

<string xmlns="http://tempuri.org/">string</string>



SOAP以POST传送XML数据,适用于大量数据传送。
他也是以XML形式返回(response)。
开发时最好有中间件来协助。


request

POST /WebSite1/WebService.asmx HTTP/1.1

Host: localhost

Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: "http://tempuri.org/getHello"

 

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

               xmlns:xsd="http://www.w3.org/2001/XMLSchema"

              xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

  <soap:Body>

    <getHello xmlns="http://tempuri.org/">

      <str>string</str>

    </getHello>

  </soap:Body>

</soap:Envelope>

response

HTTP/1.1 200 OK

Content-Type: text/xml; charset=utf-8

Content-Length: length

 

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

               xmlns:xsd="http://www.w3.org/2001/XMLSchema"

              xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

  <soap:Body>

    <getHelloResponse xmlns="http://tempuri.org/">

      <getHelloResult>string</getHelloResult>

    </getHelloResponse>

  </soap:Body>

</soap:Envelope>



JSON基本上跟REST一样,不过response值不是XML格式的而是JSON格式

转自http://blog.sina.com.cn/s/blog_5e4a47dd0100go6f.html

抱歉!评论已关闭.