Webservice
Webservice是使應用程序以與平台和編程語言無關的方式進行相互通信技術。
eg:站點提供訪問的數據接口:新浪微博、淘寶。
官方解釋:它是一種構建應用程序的普遍模型,可以在任何支持網絡通信的操作系統中實施運行;它是一種新的web應用程序分支,是自包含、自描述、模塊化的應用,可以發布、定位、通過web調用。WebService是一個應用組件,它邏輯性的為其他應用程序提供數據與服務.各應用程序通過網絡協議和規定的一些標准數據格式(Http,XML,Soap)來訪問WebService,通過WebService內部執行得到所需結果.Web Service可以執行從簡單的請求到復雜商務處理的任何功能。一旦部署以后,其他WebService應用程序可以發現並調用它部署的服務。
SOAP(Simple Object Access Protocol):簡單對象訪問協議是在分散或分布式的環境中交換信息並執行遠程過程調用的輕量級協議,是一個基於XML的協議。使用SOAP,不用考慮任何特定的傳輸協議(最常用的還是HTTP協議),可以允許任何類型的對象或代碼,在任何平台上,以任何一種語言相互通信。
WSDL:Web Services Description Language的縮寫,是一個用來描述Web服務和說明如何與Web服務通信的XML語言。為用戶提供詳細的接口說明書。
Axis:Axis本質上就是一個SOAP引擎(Apache Axis is an implementation of the SAOP),提供創建服務名、客戶端和網關SOAP操作的基本框架。但是Axis並不完全是一個SOAP引擎,它還包括:
- 是一個獨立的SOAP服務器。
- 是一個嵌入Servlet引擎(eg:Tomcat)的服務器。
- 支持WSDL。
- 提供轉化WSDL為Java類的工具。
- 提供例子程序。
- 提供TCP/IP數據包監視工具。
Axis有四種Service styles,分別是:
- RPC(Remote Procedure Call Protocol遠程訪問調用協議,部署時屬於默認選項)
- Document
- Wrapped
- Message
WSDD(Web Service Deployment Descriptor):Web服務分布描述,它定義了Web服務的接口,如服務名、提供的方法、方法的參數信息。
UDDI(Universal Description,Discovery,and Integration):統一描述、發現和集成,用於集中存放和查找WSDL描述文件,起着目錄服務器的作用。
WSDL元素
WSDL元素基於XML語法描述了與服務進行交互的基本元素:
Type(消息類型):數據類型定義的容器,它使用某種類型系統(如XSD)。
Message(消息):通信數據的抽象類型化定義,它由一個或者多個part組成。
Part:消息參數
Operation(操作):對服務所支持的操作進行抽象描述,WSDL定義了四種操作:
- 單向(one-way):端點接受信息;
- 請求-響應(request-response):端點接受消息,然后發送相關消息;
- 要求-響應(solicit-response):端點發送消息,然后接受相關消息;
- 通知(notification):端點發送消息。
Port Type (端口類型):特定端口類型的具體協議和數據格式規范。
Binding:特定端口類型的具體協議和數據格式規范
Port :定義為綁定和網絡地址組合的單個端點。
Service:相關端口的集合,包括其關聯的接口、操作、消息等。
以上類圖表達了Service、Port、Binding、Operation、Message之間的依賴、關聯、聚合、合成、泛化、實現,這里暫不多說,若感興趣,請參考該文章
UML類圖關系大全:http://www.cnblogs.com/riky/archive/2007/04/07/704298.html
WSDL偽代碼
WSDL 文檔是利用這些主要的元素來描述某個 web service 的:
元素 定義
web service 執行的操作
<message> web service 使用的消息 <types> web service 使用的數據類型 <binding> web service 使用的通信協議 一個 WSDL 文檔的主要結構是類似這樣的: <definitions> <types> definition of types........ </types> <message> definition of a message.... </message> <portType> definition of a port....... </portType> <binding> definition of a binding.... </binding> </definitions>
WSDL 文檔可包含其它的元素,比如 extension 元素,以及一個 service 元素,此元素可把若干個 web services 的定義組合在一個單一的 WSDL 文檔中
實踐
為了形成鮮明的對比,客戶端用CS架構來創建客戶端。
實踐之一:創建服務端
創建ASP.NET Web服務
代碼示例
using System; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; using DotNet.Model; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。 // [System.Web.Script.Services.ScriptService] public class Service : System.Web.Services.WebService { public Service () { //如果使用設計的組件,請取消注釋以下行 //InitializeComponent(); } [WebMethod(Description="獲取字符串",MessageName="HelloWorld")] public string HelloWorld() { return "Hello World"; } [WebMethod(Description="獲取用戶信息",MessageName="getCustomer")] public Customer getCustomer(Customer cus1) { return cus1; } [WebMethod(Description = "獲取用戶信息以參數形式", MessageName = "getCustomerFromParams")] public Customer getCustomerFromParams(int id, string name, string address) { Customer cus1 = new Customer(); cus1.cus_id = id; cus1.cus_name = name; cus1.cus_address = address; return cus1; } }
實踐之二:創建客戶端
創建ASP.NET WEB客戶端(以CS架構)
代碼示例
(其中一種方式通過創建“服務引用”的方式,輸入“http://localhost:端口號/XX.asmx?wsdl”方式,獲得服務訪問接口)
private ServiceReference1.ServiceSoapClient myclient = new ServiceReference1.ServiceSoapClient(); private void button1_Click(object sender, EventArgs e) { try { /* 第一種方式可以傳參、傳實體,返回實體 */ ServiceReference1.ServiceSoapClient myclient = new ServiceReference1.ServiceSoapClient(); ServiceReference1.Customer tem = new ServiceReference1.Customer(); tem.cus_id = int.Parse(textBox1.Text); tem.cus_name = textBox2.Text; tem.cus_address = textBox3.Text; string str = JsonHelper.Jso_ToJSON(myclient.getCustomer(tem)); richTextBox1.Text = str; } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button2_Click(object sender, EventArgs e) { richTextBox1.Text = string.Empty; try { richTextBox1.Text = myclient.HelloWorld(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button4_Click(object sender, EventArgs e) { try { /* 第二種方式可以傳參,返回實體(不能傳入一個對象實體,但是配置動態靈活) */ string url = "http://localhost:3199/ServicePort/Service.asmx"; string methodname = "getCustomerFromParams"; object[] obj = new object[3]; obj[0] = int.Parse(textBox1.Text); obj[1] = textBox2.Text; obj[2] = textBox3.Text; string str = JsonHelper.Jso_ToJSON(WebServiceHelper.InvokeWebService(url, methodname, obj)); richTextBox1.Text = str; } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button5_Click(object sender, EventArgs e) { try { /* 第三種方式可以傳參、傳實體,返回XmlDom(配置動態靈活,響應處理麻煩了點而已) */ string url = "http://localhost:3199/ServicePort/Service.asmx"; string methodname = "getCustomer"; Hashtable parm = new Hashtable(); string objectName = "cus1"; parm["cus_id"] = int.Parse(textBox1.Text); parm["cus_name"] = textBox2.Text; parm["cus_address"] = textBox3.Text; XmlDocument oo = WebServiceXmlHelper.QuerySoapWebServiceByObject(url, methodname, objectName, parm); richTextBox1.Text = oo.InnerXml; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
運行效果
服務端
客戶端(支持多平台,如服務器的Webservice用Java、.Net等)
第1種方式:傳參、傳實體,URL配置缺少靈活,數據處理靈活
第2種方式:傳參,不能傳實體,URL配置靈活, 數據處理要稍微加工
第3種方式:傳參、傳實體、URL配置靈活,數據處理要稍微加工
小結
- 如果只傳遞參數,可以用HTTP來傳遞,Webservice提供的接口如下
HTTP POST
以下是 HTTP POST 請求和響應示例。所顯示的占位符需替換為實際值。
POST /ServicePort/Service.asmx/getCustomerFromParams HTTP/1.1 Host: localhost Content-Type: application/x-www-form-urlencoded Content-Length: length id=string&name=string&address=string
返回接收串
HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <getCustomerFromParamsResponse xmlns="http://tempuri.org/"> <getCustomerFromParamsResult> <cus_id>int</cus_id> <cus_name>string</cus_name> <cus_address>string</cus_address> </getCustomerFromParamsResult> </getCustomerFromParamsResponse> </soap12:Body> </soap12:Envelope>
WebService向外發布接口的功能,能夠更好的為其它平台提供數據以及現實信息平台一體化。
Word文檔下載:淺淡Webservice、WSDL三種服務訪問的方式.doc
源代碼下載:http://files.cnblogs.com/yongfeng/001DotNetWebService.rar
參考網站:http://www.w3.org/TR/wsdl