日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

浅淡Webservice、WSDL三种服务访问的方式(附案例)

發(fā)布時間:2025/3/21 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 浅淡Webservice、WSDL三种服务访问的方式(附案例) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Webservice

Webservice是使應(yīng)用程序以與平臺和編程語言無關(guān)的方式進行相互通信技術(shù)。

eg:站點提供訪問的數(shù)據(jù)接口:新浪微博、淘寶。

官方解釋:它是一種構(gòu)建應(yīng)用程序的普遍模型,可以在任何支持網(wǎng)絡(luò)通信的操作系統(tǒng)中實施運行;它是一種新的web應(yīng)用程序分支,是自包含、自描述、模塊化的應(yīng)用,可以發(fā)布、定位、通過web調(diào)用。WebService是一個應(yīng)用組件,它邏輯性的為其他應(yīng)用程序提供數(shù)據(jù)與服務(wù).各應(yīng)用程序通過網(wǎng)絡(luò)協(xié)議和規(guī)定的一些標準數(shù)據(jù)格式(Http,XML,Soap)來訪問WebService,通過WebService內(nèi)部執(zhí)行得到所需結(jié)果.Web Service可以執(zhí)行從簡單的請求到復(fù)雜商務(wù)處理的任何功能。一旦部署以后,其他WebService應(yīng)用程序可以發(fā)現(xiàn)并調(diào)用它部署的服務(wù)。

?

SOAP(Simple Object Access Protocol):簡單對象訪問協(xié)議是在分散或分布式的環(huán)境中交換信息并執(zhí)行遠程過程調(diào)用的輕量級協(xié)議,是一個基于XML的協(xié)議。使用SOAP,不用考慮任何特定的傳輸協(xié)議(最常用的還是HTTP協(xié)議),可以允許任何類型的對象或代碼,在任何平臺上,以任何一種語言相互通信。

WSDL:Web Services Description Language的縮寫,是一個用來描述Web服務(wù)和說明如何與Web服務(wù)通信的XML語言。為用戶提供詳細的接口說明書。

Axis:Axis本質(zhì)上就是一個SOAP引擎(Apache Axis is an implementation of the SAOP),提供創(chuàng)建服務(wù)名、客戶端和網(wǎng)關(guān)SOAP操作的基本框架。但是Axis并不完全是一個SOAP引擎,它還包括:

  • 是一個獨立的SOAP服務(wù)器。
  • 是一個嵌入Servlet引擎(eg:Tomcat)的服務(wù)器。
  • 支持WSDL。
  • 提供轉(zhuǎn)化WSDL為Java類的工具。
  • 提供例子程序。
  • 提供TCP/IP數(shù)據(jù)包監(jiān)視工具。

Axis有四種Service styles,分別是:

  • RPC(Remote Procedure Call Protocol遠程訪問調(diào)用協(xié)議,部署時屬于默認選項)
  • Document
  • Wrapped
  • Message

WSDD(Web Service Deployment Descriptor):Web服務(wù)分布描述,它定義了Web服務(wù)的接口,如服務(wù)名、提供的方法、方法的參數(shù)信息。

UDDI(Universal Description,Discovery,and Integration):統(tǒng)一描述、發(fā)現(xiàn)和集成,用于集中存放和查找WSDL描述文件,起著目錄服務(wù)器的作用。

?

?

WSDL元素

WSDL元素基于XML語法描述了與服務(wù)進行交互的基本元素:

Type(消息類型):數(shù)據(jù)類型定義的容器,它使用某種類型系統(tǒng)(如XSD)。

Message(消息):通信數(shù)據(jù)的抽象類型化定義,它由一個或者多個part組成。

Part:消息參數(shù)

Operation(操作):對服務(wù)所支持的操作進行抽象描述,WSDL定義了四種操作:

  • 單向(one-way):端點接受信息;
  • 請求-響應(yīng)(request-response):端點接受消息,然后發(fā)送相關(guān)消息;
  • 要求-響應(yīng)(solicit-response):端點發(fā)送消息,然后接受相關(guān)消息;
  • 通知(notification):端點發(fā)送消息。

Port Type (端口類型):特定端口類型的具體協(xié)議和數(shù)據(jù)格式規(guī)范。

Binding:特定端口類型的具體協(xié)議和數(shù)據(jù)格式規(guī)范

Port :定義為綁定和網(wǎng)絡(luò)地址組合的單個端點。

Service:相關(guān)端口的集合,包括其關(guān)聯(lián)的接口、操作、消息等。

?

以上類圖表達了Service、Port、Binding、Operation、Message之間的依賴、關(guān)聯(lián)、聚合、合成、泛化、實現(xiàn),這里暫不多說,若感興趣,請參考該文章

UML類圖關(guān)系大全:http://www.cnblogs.com/riky/archive/2007/04/07/704298.html

WSDL偽代碼

WSDL 文檔是利用這些主要的元素來描述某個 web service 的:

元素 定義

web service 執(zhí)行的操作

<message> web service 使用的消息 <types> web service 使用的數(shù)據(jù)類型 <binding> web service 使用的通信協(xié)議 一個 WSDL 文檔的主要結(jié)構(gòu)是類似這樣的: <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架構(gòu)來創(chuàng)建客戶端。

實踐之一:創(chuàng)建服務(wù)端

創(chuàng)建ASP.NET Web服務(wù)

?

代碼示例

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 從腳本中調(diào)用此 Web 服務(wù),請取消對下行的注釋。 // [System.Web.Script.Services.ScriptService] public class Service : System.Web.Services.WebService {public Service () {//如果使用設(shè)計的組件,請取消注釋以下行//InitializeComponent(); }[WebMethod(Description="獲取字符串",MessageName="HelloWorld")]public string HelloWorld() {return "Hello World";}[WebMethod(Description="獲取用戶信息",MessageName="getCustomer")]public Customer getCustomer(Customer cus1){return cus1;}[WebMethod(Description = "獲取用戶信息以參數(shù)形式", 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;}}

?

實踐之二:創(chuàng)建客戶端

創(chuàng)建ASP.NET WEB客戶端(以CS架構(gòu))

?

代碼示例

?(其中一種方式通過創(chuàng)建“服務(wù)引用”的方式,輸入“http://localhost:端口號/XX.asmx?wsdl”方式,獲得服務(wù)訪問接口)

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{/* 第二種方式可以傳參,返回實體(不能傳入一個對象實體,但是配置動態(tài)靈活) */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(配置動態(tài)靈活,響應(yīng)處理麻煩了點而已) */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);}}

?

運行效果

服務(wù)端

?

?

WSDL

?

客戶端(支持多平臺,如服務(wù)器的Webservice用Java、.Net等)

第1種方式:傳參、傳實體,URL配置缺少靈活,數(shù)據(jù)處理靈活

?

第2種方式:傳參,不能傳實體,URL配置靈活, 數(shù)據(jù)處理要稍微加工

?

第3種方式:傳參、傳實體、URL配置靈活,數(shù)據(jù)處理要稍微加工

?

小結(jié)

  • 如果只傳遞參數(shù),可以用HTTP來傳遞,Webservice提供的接口如下

HTTP POST

以下是 HTTP POST 請求和響應(yīng)示例。所顯示的占位符需替換為實際值。

POST /ServicePort/Service.asmx/getCustomerFromParams HTTP/1.1 Host: localhost Content-Type: application/x-www-form-urlencoded Content-Length: lengthid=string&name=string&address=string

?

返回接收串

HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <Customer xmlns="http://tempuri.org/"><cus_id>int</cus_id><cus_name>string</cus_name><cus_address>string</cus_address> </Customer>

?

?

  • 如果是傳遞實體或傳遞參數(shù),也可以使用SOAP來傳遞,Webservice提供的接口如下
POST /ServicePort/Service.asmx HTTP/1.1 Host: localhost 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><getCustomerFromParams xmlns="http://tempuri.org/"><id>int</id><name>string</name><address>string</address></getCustomerFromParams></soap12:Body> </soap12:Envelope>

?

?

返回接收串:

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向外發(fā)布接口的功能,能夠更好的為其它平臺提供數(shù)據(jù)以及現(xiàn)實信息平臺一體化。

Word文檔下載:淺淡Webservice、WSDL三種服務(wù)訪問的方式.doc

源代碼下載:http://files.cnblogs.com/yongfeng/001DotNetWebService.rar

參考網(wǎng)站:http://www.w3.org/TR/wsdl


from:?http://www.cnblogs.com/yongfeng/archive/2013/01/30/2883146.html

總結(jié)

以上是生活随笔為你收集整理的浅淡Webservice、WSDL三种服务访问的方式(附案例)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。