`
hideto
  • 浏览: 2652349 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

读 RESTful Web Service 第二章

阅读更多
Chapter2,Writing Web Service Clients

Web Services Are Web Sites
Yahoo!提供了RESTful Web Service,用Ruby来调用该服务非常简单:
require 'open-uri'
require 'rexml/document'
require 'cgi'

BASE_URI = 'http://api.search.yahoo.com/WebSearchService/V1/webSearch'

def print_page_titles(term)
  term = CGI::escape(term)
  xml = open(BASE_URI + "?appid=restbook&query=#{term}").read
  document = REXML::Document.new(xml)
  REXML::XPath.each(document, '/ResultSet/Result/Title/[]') do |title|
    puts title
  end
end

print_page_titles(ARGV.join(' '))

Google则还是RPC风格的SOAP,不过最近貌似加上了Ajax风格的Web服务?
既然大家都发布了服务API,我要是使用自己的服务器假设一个伪搜索引擎岂不是很简单?不知道是否侵权哦?
RESTful Web Service非常简单,我们只需用某种编程语言的HTTP客户端库即可

Wrappers, WADL, and ActiveResource
What is WSDL?
WSDL stands for Web Services Description Language
WSDL is written in XML
WSDL is an XML document
WSDL is used to describe Web services
WSDL is also used to locate Web services
WSDL is not yet a W3C standard

What is SOAP?
SOAP stands for Simple Object Access Protocol
SOAP is a communication protocol
SOAP is for communication between applications
SOAP is a format for sending messages
SOAP is designed to communicate via Internet
SOAP is platform independent
SOAP is language independent
SOAP is based on XML
SOAP is simple and extensible
SOAP allows you to get around firewalls
SOAP will be developed as a W3C standard

SOAP类似于HTTP,都是底层通信协议,尤其是SOAP与HTTP、SMTP等绑定时。
而WSDL是描述Web服务的,我们针对一个WSDL文件调用一个Web服务时,底层通信就是遵守SOAP协议的

WADL(Web Application Description Language) is an XML-based file format that provides a machine-readble description
of HTTP-based web applications. These applications are typically RESTful web services.

WSDL用来描述基于SOAP协议的Web Service,而WADL用来描述RESTful Web Service

对不同的RESTful Web Service我们可以使用不同的Wrappers来封装以供调用(如上面的print_page_titles就是一个封装),但是这对不同的
RESTful Web Service不通用,所以WADL的出现就是为了对不同的RESTful Web Service提供一致的描述

ActiveResource则是针对Ruby on Rails写的RESTful Web Service的client端调用封装

del.icio.us: The Sample Application
del.icio.us的Web服务设计不是RESTful的,它始终使用GET方法,而且“add”和“rename”等操作都是嵌在URI中而不是使用HTTP方法

Making the Request: HTTP Libraries
构建Web服务客户端要求HTTP库至少满足以下特性:
支持HTTPS和SSL
支持GET、HEAD、POST、PUT和DELETE
支持程序员自定义PUT或POST请求的entity-body
支持程序员自定义请求的HTTP headers
支持HTTP应答的response code和headers
支持HTTP proxy

Ruby: rest-open-uri、net/http和net/https
Python: httplib2
Java: HttpClient
C#: System.Web.HTTPWebRequest
PHP: libcurl
JavaScript: XMLHttpRequest
Command Line: curl
ActionScript: XML
C: libwww、libcurl
C++: libcurl、cURLpp
Common Lisp: simple-http、AllegroServe
Perl: libwww-perl、Crypt:SSLeay

Processing the Response: XML Parsers
三种XMl Parsers
DOM: document-based、tree-style,读取整个XML文件后再开始解析,可以自由处理任何document内容
SAX: event-based、register callback methods,将document转变为一系列event,按顺序处理每个event,不能在处理中stop
Pull: event-based、一次处理一个event,需要时再pull下一个event,可以在任何时候stop

Ruby: REXML
Python: ElementTree
Java: javax.xml、Xerces、XMLPull
C#: System.Xml.XmlReader
PHP: XmlReader、DOM、SimpleXMl、DOMIT
JavaScript: responseXML
ActionScript: XML
C: Expat、libxml2
C++: Xerces-C++
Common Lisp: SXML
Perl: XML::XPath、XML::SAX::PurePerl、XML::LibXML::Reader

JSON Parsers: Handling Serialized Data
JSON的缺点:
不支持对象图和循环引用(使用外键来处理)
不支持类信息(附加类名)

Ruby: Object#to_json、JSON.parse
dojo: dojo.json

Clients Made Easy with WADL
作者是推荐使用WADL来描述RESTful Web Service,就像WSDL描述SOAP Web Service一样
不过像Rails的REST实现就只是用了简单的Object#to_xml来作为RESTful Web Service的描述,简单至极
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics