欢迎光临
我们一直在努力

用WSDL和代理类创建可编程WEB服务-.NET教程,Web Service开发

建站超值云服务器,限时71元/月

在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中调试通过。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用WSDL和代理类创建可编程WEB服务-.NET教程,Web Service开发
分享到: 更多 (0)

相关推荐

  • 暂无文章