WebService:简介

来源:互联网 发布:seo sem 新媒体 电商 编辑:程序博客网 时间:2024/06/04 01:36

现在的应用程序变得越来越复杂,甚至只靠单一的应用程序无法完成全部的工作。更别说只使用一种语言了。Webservice实现了跨平台、跨语言的应用之间的远程调用,做到了应用的模块化和独立化,一个应用只需要暴露出他需要暴露的东西,别人就可以直接调用它,别人不需要知道关于他实现的细节。比如天气预报、手机短信的发送、翻译服务等。实现远程调用也可以使用RMI(java远程方法调用协议),但是webservice更能做到跨语言,两种语言之间怎么实现互相调用呢?——中间通过都能解析的中间语言(XML)。一个资源较多并且免费的网站:www.webxml.com.cn。开发微信公众号的时候就用到了巨多的web服务(当然有的时候使用的是xml,有的时候用的又是json)。不管是http、webservice、RMI还是什么,只要涉及到网络,其底层肯定都是使用socket,建立在tcp/ip或者UDP协议之上,都属于应用层协议。服务端使用ServerSocket获取连接,客户端使用Socket与服务端通信。

l  写应用程序查询数据库时,并没有考虑过为什么可以将查询结果返回给上层的应用程序,甚至认为,这就是数据库应该做的,其实不然,这是数据库通过TCP/IP协议与另一个应用程序进行交流的结果,而上层是什么样的应用程序,是用什么语言,数据库本身并不知道,它只知道接收到了一份协议,这就是SQL92查询标准协议。

l  目前的云计算、云查杀都是一种服务,现在比较流行的说法是SOA(面向服务的框架)。

l  既然数据库可以依据某些标准对外部其他应用程序提供服务、而且不关心对方使用什么语言,那我们为什么就不能实现跨平台、跨语言的服务呢?只要我们用Java写的代码,可以被任意的语言所调用,我们就实现了跨平台,跨语言的服务!

l  从WebService的工作模式上理解的话,它跟普通的Web程序(比如ASP、JSP等)并没有本质的区别,都是基于HTTP传输协议的程序。

l  WebService所使用的数据均是基于XML格式的。目前标准的WebService在数据格式上主要采用SOAP协议。SOAP协议实际上就是一种基于XML编码规范的文本协议。

 

 

l  名词1:XML.Extensible Markup Language -扩展性标记语言

•    XML,用于传输格式化的数据,是Web服务的基础。

•   namespace-命名空间。

•   xmlns=“http://itcast.cn”使用默认命名空间。

•   xmlns:itcast=“http://itcast.cn”使用指定名称的命名空间。

l  名词2:WSDL –WebService Description Language – Web服务描述语言。

•   通过XML形式说明服务在什么地方-地址。

•   通过XML形式说明服务提供什么样的方法如何调用。

l  名词3:SOAP-SimpleObject Access Protocol(简单对象访问协议)

•    SOAP作为一个基于XML语言的协议用于有网上传输数据。

•   SOAP =HTTP的基础上+XML数据。

•    SOAP是基于HTTP的。

•    SOAP的组成如下:

•    Envelope – 必须的部分。以XML的根元素出现。

•    Headers – 可选的。

•    Body – 必须的。在body部分,包含要执行的服务器的方法。和发送到服务器的数据。

下面是一个wsdl文档,其实就是告诉你我的服务在哪儿?你怎么调用?

 

<?xml version="1.0"encoding="UTF8" ?>

<definitionsxmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"

         xmlns:tns="http://ws.itcast.cn/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"

         xmlns="http://schemas.xmlsoap.org/wsdl/"targetNamespace="http://ws.itcast.cn/"

         name="HelloServiceService">

         <types>

                   <xsd:schema>

                            <xsd:importnamespace="http://ws.itcast.cn/"

                                     schemaLocation="http://localhost:9999/hello?xsd=1"/>

                   </xsd:schema>

         </types>

         <messagename="sayHi">

                   <partname="parameters" element="tns:sayHi" />

         </message>

         <messagename="sayHiResponse">

                   <partname="parameters" element="tns:sayHiResponse" />

         </message>

         <portTypename="HelloService">

                   <operationname="sayHi">【服务提供的方法】

                            <inputmessage="tns:sayHi" />【参数输入】

                            <outputmessage="tns:sayHiResponse" />【参数输出】

                   </operation>

         </portType>

         <bindingname="HelloServicePortBinding" type="tns:HelloService">

                   <soap:bindingtransport="http://schemas.xmlsoap.org/soap/http"

                            style="document"/>

                   <operationname="sayHi">

                            <soap:operationsoapAction="" />

                            <input>

                                     <soap:bodyuse="literal" />

                            </input>

                            <output>

                                     <soap:bodyuse="literal" />

                            </output>

                   </operation>

         </binding>

         <servicename="HelloServiceService">【服务的名称】

                   <portname="HelloServicePort" binding="tns:HelloServicePortBinding">

                            <soap:addresslocation="http://localhost:9999/hello" />【服务的地址】

                   </port>

         </service>

</definitions>

 

下面是SOAP协议的范本

请求【因为是在HTTP的基础上的协议,所以首先应该遵守HTTP协议

POST /WebServices/MobileCodeWS.asmxHTTP/1.1

Host: webservice.webxml.com.cn

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

Content-Length: length

SOAPAction: http://WebXml.com.cn/getMobileCodeInfo

 

<?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>

    <getMobileCodeInfoxmlns="http://WebXml.com.cn/">

     <mobileCode>string</mobileCode>

     <userID>string</userID>

   </getMobileCodeInfo>

 </soap:Body>

</soap:Envelope>

 

响应

 

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>

   <getMobileCodeInfoResponsexmlns="http://WebXml.com.cn/">

     <getMobileCodeInfoResult>string</getMobileCodeInfoResult>

   </getMobileCodeInfoResponse>

 </soap:Body>

</soap:Envelope>

            ---UDDI


 

0 0
原创粉丝点击