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
我們在textbox中輸入“中國”,再點擊button,於是就可以在下邊的textbox中顯示“hello world, 中國” 。顯示如圖: