** 在 ** ** VB6 ** ** 或 ** ** ASP ** ** 中调用 ** ** webservice **
Web Services 技术使异种计算环境之间可以共享数据和通信,达到信息的一致性。我们可以利用
HTTP POST/GET 协议、 SOAP 协议来调用 Web Services 。
一、 利用 SOAP 协议在 VB6 中调用 Web Services
; 首先利用 .net 发布一个简单的 Web Services
1<webmethod()> _
2
3Public Function getString( ByVal str As String ) As String
4
5Return "Hello World, " & str & " ! "
6
7End Function
8
9该 Web Services 只包含一个 _ getString _ 方法,用于返回一个字符串。当我们调用这个 Web Services 时,发送给 .asmx 页面的 SOAP 消息为:
10
11
12 <?xml version="1.0" encoding="utf-8"?>
13<soap:envelope xmlns:soap="http://
14
15
16 schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
17
18
19 <soap:body>
20
21
22 <getstring xmlns="http://tempuri.org/TestWebService/Service1">
23
24
25 <str> **string** </str>
26
27
28 </getstring>
29
30
31 </soap:body>
32</soap:envelope>
33
34而返回的 SOAP 消息为:
35
36
37 <?xml version="1.0" encoding="utf-8"?>
38<soap:envelope xmlns:soap="
39
40
41 http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
42
43
44 <soap:body>
45
46
47 <getstringresponse xmlns="http://tempuri.org/TestWebService/Service1">
48
49
50 <getstringresult> **string** </getstringresult>
51
52
53 </getstringresponse>
54
55
56 </soap:body>
57</soap:envelope>
58
59###
60
61; 在 VB6 中调用这个简单的 Web Services 可以利用利用 XMLHTTP 协议向 .asmx 页面发
62
63送 SOAP 来实现。
64
65在 VB6 中, 建立一个简单的工程,界面如图,我们通过点击 Button 来调用 这个简
66
67单的 Web Services
68
69
70
71Dim strxml As String
72
73Dim str As String
74
75str = Text2.Text
76
77'定义soap消息
78
79strxml = "<?xml version='1.0' encoding='utf-8'?><soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:body><getstring xmlns="http://tempuri.org/TestWebService/Service1"><str>" & str &
80
81"</str></getstring></soap:body></soap:envelope>"
82
83' 定义一个 http 对象,一边向服务器发送 post 消息
84
85Dim h As MSXML2.ServerXMLHTTP40
86
87' 定义一个 XML 的文档对象,将手写的或者接受的 XML 内容转换成 XML 对象
88
89Dim x As MSXML2.DOMDocument40
90
91' 初始化 XML 对象
92
93Set x = New MSXML2.DOMDocument40
94
95' 将手写的 SOAP 字符串转换为 XML 对象
96
97x.loadXML strxml
98
99' 初始化 http 对象
100
101Set h = New MSXML2.ServerXMLHTTP40
102
103' 向指定的 URL 发送 Post 消息
104
105h.open "POST", "http://localhost/TestWebService/Service1.asmx", False
106
107h.setRequestHeader "Content-Type", "text/xml"
108
109h.send (strxml)
110
111While h.readyState <> 4
112
113Wend
114
115' 显示返回的 XML 信息
116
117Text1.Text = h.responseText
118
119' 将返回的 XML 信息解析并且显示返回值
120
121Set x = New MSXML2.DOMDocument40
122
123x.loadXML Text1.Text
124
125Text1.Text = x.childNodes(1).Text
126
127我们在 TEXTBOX 中输入“中国”,再点击 BUTTON ,于是就可以在下边的 TEXTBOX 中显示“ Hello World, 中国” 。显示如图:
128
129</webmethod()>