国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

用WSDL和代理類創建可編程WEB服務

2024-07-21 02:21:38
字體:
來源:轉載
供稿:網友
在asp.net中,我們可以創建wsdl文件來描述當前提供的html或xml或者任何其他非二進制格式)頁,可以使用wsdl來生成客戶端代理,并使用visual studio.net或wsdl.exe命令行工具創建代理類。最后通過 regex 來分析已命名的html頁和提取值。以下介紹完整的實現過程:

一、為網站編寫wsdl文件

我們以訪問http://movies.yahoo.com/電影網站中“本周票房排行榜”(top box office)的數據為例,檢索出票房排名第一的影片名稱。

通過查看http://movies.yahoo.com/網頁的html源文件,可以看到排名第一影片的鏈接是:finding nemo,為此可在 wsdl 的響應節中添加 標記。這些標記采用一個稱為 pattern 的屬性,這是與頁面上作為屬性值的文本部分相對應的正則表達式。這里我們創建的正規表達式是:“pattern="d=hv&cf=info&id=[0-9]*">(.*?)
<?xml version="1.0" encoding="gb2312"?>
<definitions xmlns:s="http://www.w3.org/2000/10/xmlschema"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:s0="http://tempuri.org/"
targetnamespace="http://tempuri.org/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:mstype="http://microsoft.com/wsdl/mime/textmatching/">
<types/>
<message name="getbookdetailshttpgetin">
<part name="isbn" type="s:string"/>
</message>
<message name="getbookdetailshttpgetout"/>
<porttype name="barnesandnoblehttpget">
<operation name="getbookdetails">
<input message="s0:getbookdetailshttpgetin"/>
<output message="s0:getbookdetailshttpgetout"/>
</operation>
</porttype>
<binding name="barnesandnoblehttpget" type="s0:barnesandnoblehttpget">
<http:binding verb="get"/>
<operation name="getbookdetails">
<http:operation location=""/>
<input>
<http:urlencoded/>
</input>
<output>
<mstype:text>
<mstype:match name="rank" pattern="d=hv&cf=info&id=[0-9]*">(.*?)
</"ignorecase="true"/>
</mstype:text>
</output>
</operation>
</binding>
<service name="barnesandnoble">
<port name="barnesandnoblehttpget" binding="s0:barnesandnoblehttpget">
<http:address location="http://movies.yahoo.com/"/>
</port>
</service>
</definitions>



在上面的wsdl中,定義了barnesandnoble類,指定進行檢索的站點http://movies.yahoo.com/,由于是一般的通用網站,此服務不使用soap,而是使用http get進行請求。

二、構建web服務代理

在visual studio.net中,右鍵單擊“解決方案資源管理器”中的“引用”,選擇“添加web引用”,就可以打開“添加web引用”對話框,

在此對話框中,輸入剛才創建好的wsdl文件所在的地址,visual studio.net從指定的位置獲取wsdl并驗證它。單擊“添加引用”按鈕,就可以將此wsdl描述的web服務的引用添加到當前的工程中。

通過以上操作,visual studio.net在后臺自動分析wsdl,并創建了代表web服務的代理對象,并高速緩存了wsdl的本地副本。如果wsdl內容發生變化,需要手工“更新web引用”。

web服務代理的源代碼如下:

public class barnesandnoble
inherits system.web.services.protocols.httpgetclientprotocol
'<remarks/>
public sub new()
mybase.new
me.url = "http://movies.yahoo.com/"
end sub
'<remarks/>
<system.web.services.protocols.httpmethodattribute(gettype
(system.web.services.protocols.textreturnreader), gettype
(system.web.services.protocols.urlparameterwriter))> _
public function getbookdetails(byval isbn as string)
as getbookdetailsmatches
return ctype(me.invoke("getbookdetails", (me.url + ""),
new object() {isbn}),getbookdetailsmatches)
end function
'<remarks/>
public function begingetbookdetails(byval isbn as string,
byval callback as system.asynccallback, byval asyncstate as object)
as system.iasyncresult
return me.begininvoke("getbookdetails", (me.url + ""),
new object() {isbn}, callback, asyncstate)
end function
'<remarks/>
public function endgetbookdetails(byval asyncresult as system.iasyncresult)
as getbookdetailsmatches
return ctype(me.endinvoke(asyncresult),getbookdetailsmatches)
end function
end class
public class getbookdetailsmatches
<system.web.services.protocols.matchattribute("d=hv&cf=info&id=[0-9]*"">
(.*?)</", lgnorecase:=true)> _
public rank as string
end class



如果在“解決方案資源管理器”中展開“web references”部分,可以看出具體表達方式:

三、在web應用程序中編寫代碼,使用barnesandnoble web服務。

private sub button1_click(byval sender as system.object,
byval e as system.eventargs) handles button1.click
dim bn as new localhost.barnesandnoble()
dim match as localhost.getbookdetailsmatches
match = bn.getbookdetails("")
rank.text = match.rank
end sub

在以上程序中,首先調用new localhost.barnesandnoble(),創建代理的一個范例bn。bn再調用getbookdetails()方法傳入參數,最后訪回一個rank值(排名第一的影片名稱)。

通過編寫wsdl,訪問由 wsdl 中的功能化名稱調用的 matches 對象,就可以將任何 html 部分作為屬性來訪問,我們可以輕松地將web站點轉換為web服務。以上程序在windows2000 server、visual studio.net中調試通過。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 叙永县| 桓台县| 通河县| 濉溪县| 连云港市| 巨野县| 随州市| 乐业县| 中江县| 庆安县| 中宁县| 无锡市| 西平县| 广汉市| 湖口县| 兰考县| 安吉县| 永新县| 海原县| 汤阴县| 秀山| 小金县| 新营市| 合川市| 开化县| 团风县| 和田县| 佛教| 崇义县| 新安县| 常山县| 孟州市| 南江县| 保定市| 霍城县| 古浪县| 绩溪县| 策勒县| 延吉市| 丹巴县| 德江县|