** 1. 在 JavaScript 中调用 WebService **
1<script language="javascript">
2
3function PostRequestData(URL,data){
4
5var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
6
7xmlhttp.Open("POST",URL, false);
8
9xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
10
11xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/myService/test/isNumner");
12
13try {
14
15xmlhttp.Send(data);
16
17var result = xmlhttp.status;
18
19}
20
21catch(ex) {
22
23return("0" + ex.description + "|" + ex.number);
24
25}
26
27if(result==200) {
28
29return("1" + xmlhttp.responseText);
30
31}
32
33xmlhttp = null;
34
35}
36
37function loadit(value){
38
39var url = 'http://localhost/myService/test.asmx';
40
41var data ;
42
43var r;
44
45data = '<?xml version="1.0" encoding="utf-8"?>';
46
47data = data + '<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/">';
48
49data = data + '<soap:Body>';
50
51data = data + '<isNumner xmlns="http://tempuri.org/myService/test">';
52
53data = data + '<str>'+value+'</str>';
54
55data = data + '</isNumner>';
56
57data = data + '</soap:Body>';
58
59data = data + '</soap:Envelope>';
60
61r=PostRequestData(url,data);
62
63document.write(r);
64
65}
66
67loadit('5');
68
69</script>
还可以使用微软的htc组件来实现,可以到这里下载:
http://msdn.microsoft.com/workshop/author/webservice/webservice.htc
1<script language="javascript">
2
3function timer(){
4
5service.useService("http://localhost/myService/test.asmx?WSDL","test");
6
7service.test.callService(callback,"isNumner",'gdh');
8
9}
10
11function callback(res){
12
13if (!res.error)
14
15time.innerText=res.value;
16
17}
18
19</script>
1<div id="service" style="behavior:url(webservice.htc)"></div>
1<span id="time"></span>
** 2. 在 Asp 中 **
1@LANGUAGE="VBSCRIPT" CODEPAGE="936"
1
2
3Dim strxml
4
5Dim str
6
7' 定义 soap 消息
8
9strxml = "
10<?xml version='1.0' encoding='tf-8'?>
11"
12
13strxml = strxml & "
<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">"
strxml = strxml & "<soap:body> "
strxml = strxml & "<isnumner xmlns="http://tempuri.org/myService/test">"
strxml = strxml & "<str>4</str>"
strxml = strxml & "</isnumner>"
strxml = strxml & "</soap:body>"
strxml = strxml & "</soap:envelope>
1"
2
3' 定义一个 XML 的文档对象,将手写的或者接受的 XML 内容转换成 XML 对象
4
5'set x = createobject("Microsoft.DOMDocument")
6
7' 初始化 XML 对象
8
9' 将手写的 SOAP 字符串转换为 XML 对象
10
11' x.loadXML strxml
12
13' 初始化 http 对象
14
15Set h = createobject( "Microsoft.XMLHTTP")
16
17' 向指定的 URL 发送 Post 消息
18
19h.open "POST", "http://localhost/myService/test.asmx", False
20
21h.setRequestHeader "Content-Type", "text/xml"
22
23h.setRequestHeader "SOAPAction", "http://tempuri.org/myService/test/isNumner"
24
25h.send (strxml)
26
27While h.readyState <> 4
28
29Wend
30
31' 显示返回的 XML 信息
32
33str = h.responseText
34
35' 将返回的 XML 信息解析并且显示返回值
36
37'Set x = createobject("MSXML2.DOMDocument")
38
39' x.loadXML str
40
41'str = x.childNodes(1).Text
42
43response.write(str)
3.在.net中
在.net中调用WebService就方便多了,没有必要自己写soap消息了,以上都是用XMLHTTP来发送WebService请求的,在.net只要添加了web引用,会自动为你创建一个代理类。然后使用代理类就像用自己定义的类一样方便。