vb6或asp中调用webservice
web services技术使异种计算环境之间可以共享数据和通信,达到信息的一致性。我们可以利用
http post/get协议、soap协议来调用web services。
一、 利用soap协议在vb6中调用web services
; 首先利用.net发布一个简单的web services
<webmethod()> _
public function getstring(byval str as string) as string
return "hello world, " & str & "!"
end function
该web services只包含一个getstring方法,用于返回一个字符串。当我们调用这个web services时,发送给.asmx页面的soap消息为:
<?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>
<getstring xmlns="http://tempuri.org/testwebservice/service1">
<str>string</str>
</getstring>
</soap:body>
</soap:envelope>
而返回的soap消息为:
<?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>
<getstringresponse xmlns="http://tempuri.org/testwebservice/service1">
<getstringresult>string</getstringresult>
</getstringresponse>
</soap:body>
</soap:envelope>
; 在vb6中调用这个简单的web services可以利用利用xmlhttp协议向.asmx页面发
送soap来实现。
在vb6中,建立一个简单的工程,界面如图,我们通过点击button来调用这个简
单的web services
dim strxml as string
dim str as string
str = text2.text
定义soap消息
strxml = "<?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><getstring xmlns=http://tempuri.org/testwebservice/service1><str>" & str &
"</str></getstring></soap:body></soap:envelope>"
定义一个http对象,一边向服务器发送post消息
dim h as msxml2.serverxmlhttp40
定义一个xml的文档对象,将手写的或者接受的xml内容转换成xml对象
dim x as msxml2.domdocument40
初始化xml对象
set x = new msxml2.domdocument40
将手写的soap字符串转换为xml对象
x.loadxml strxml
初始化http对象
set h = new msxml2.serverxmlhttp40
向指定的url发送post消息
h.open "post", "http://localhost/testwebservice/service1.asmx", false
h.setrequestheader "content-type", "text/xml"
h.send (strxml)
while h.readystate <> 4
wend
显示返回的xml信息
text1.text = h.responsetext
将返回的xml信息解析并且显示返回值
set x = new msxml2.domdocument40
x.loadxml text1.text
text1.text = x.childnodes(1).text