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
- POST /RPC2 HTTP/1.0
- User-Agent: Frontier/5.1.2 (WinNT)
- Host: betty.userland.com
- Content-Type: text/xml
- Content-length: 181
- <?xml version="1.0"?>
- <methodCall>
- <methodName>examples.getStateName</methodName>
- <params>
- <param>
- <value><i4>41</i4></value>
- </param>
- </params>
- </methodCall>
SOAP:
- POST /StockQuote HTTP/1.1
- Host: www.stockquoteserver.com
- Content-Type: text/xml; charset="utf-8"
- Content-Length: nnnn
- SOAPAction: "Some-URI"
- <SOAP-ENV:Envelope
- xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
- SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
- <SOAP-ENV:Header>
- <t:Transaction
- xmlns:t="some-URI"
- SOAP-ENV:mustUnderstand="1">
- 5
- </t:Transaction>
- </SOAP-ENV:Header>
- <SOAP-ENV:Body>
- <m:GetLastTradePrice xmlns:m="Some-URI">
- <symbol>DEF</symbol>
- </m:GetLastTradePrice>
- </SOAP-ENV:Body>
- </SOAP-ENV:Envelope>
JSON:
- --> { "method": "echo", "params": ["Hello JSON-RPC"], "id": 1}
- <-- { "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"
</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"
</soap:Envelope> |
JSON基本上跟REST一样,不过response值不是XML格式的而是JSON格式