微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[转]几种调用WebService的方法

1.  在JavaScript中调用WebService

 1

< script language = " javascript " >
 2

function  PostRequestData(URL,data){
 3

       
var  xmlhttp  =   new  ActiveXObject( " Microsoft.XMLHTTP " );
 4

       xmlhttp.Open(
" POST " ,URL,  false );
 5

       xmlhttp.SetRequestHeader (
" Content-Type " , " text/xml; charset=utf-8 " );
 6

       xmlhttp.SetRequestHeader (
" SOAPAction " , " http://tempuri.org/myService/test/isNumner " );
 7

       
 8

       
try  { 
 9

              xmlhttp.Send(data); 
10

              
var  result  =  xmlhttp.status;
11

       }
12

       
catch (ex) {
13

              
return ( " 0 "   +  ex.description  +   " | "   +  ex.number); 
14

       }
15

       
if (result == 200 ) { 
16

              
return ( " 1 "   +  xmlhttp.responseText); 
17

       }
18

       xmlhttp 
=   null ;
19

}
20

 
21

function  loadit(value){
22

       
var  url  =  'http: // localhost/myService/test.asmx';
23

        var  data ;
24

       
var  r;
25

       
26

       data 
=  ' <? xml version = " 1.0 "  encoding = " utf-8 " ?> ';
27

       data 
=  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/ " > ';
28

    data 
=  data  +  ' < soap:Body > ';
29

    data 
=  data  +  ' < isNumner xmlns = " http://tempuri.org/myService/test " > ';
30

    data 
=  data  +  ' < str > ' + value + ' </ str > ';
31

    data 
=  data  +  ' </ isNumner > ';
32

    data 
=  data  +  ' </ soap:Body > ';
33

    data 
=  data  +  ' </ soap:Envelope > ';
34

       
35

       r
= PostRequestData(url,data);
36

       document.write(r);                
37

}
38

loadit('
5 ');
39

</ script >

 还可以使用微软的htc组件来实现,可以到这里下载: 
http://msdn.microsoft.com/workshop/author/webservice/webservice.htc
<script language="javascript">
    function timer(){
        service.useService(" http://localhost/myService/test.asmx?WSDL","test");
        service.test.callService(callback,"isNumner",'gdh');
     }
    
    function callback(res){
        if (!res.error)
            time.innerText=res.value;                
    }
 </script>
 
<div id="service" style="behavior:url(webservice.htc)"></div>
<span id="time"></span>

2. 在Asp中
 1

< %@LANGUAGE = " VBSCRIPT "  CODEPAGE = " 936 " % >
 2

< %
 3

             
Dim  strxml 
 4

        
Dim  str       
 5

       
 6

        
' 定义soap消息
 7

        strxml  =   " <?xml version='1.0' encoding='tf-8'?>"
 8

        strxml  =  strxml  &   " <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/'>"
 9

        strxml  =  strxml  &   " <soap:Body> "
10

              strxml  =  strxml  &   " <isNumner xmlns='http://tempuri.org/myService/test'>"
11

        strxml  =  strxml  &   " <str>4</str> "  
12

        strxml 
=  strxml  &   " </isNumner>"
13

              strxml  =  strxml  &   " </soap:Body> "  
14

        strxml 
=  strxml  &   " </soap:Envelope>"
15

              
16

        
' 定义一个XML的文档对象,将手写的或者接受的XML内容转换成XML对象
17

         ' set x = createobject("Microsoft.DOMDocument")
18

         ' 初始化XML对象
19

         ' 将手写的SOAP字符串转换为XML对象
20

         '  x.loadXML strxml
21

         ' 初始化http对象
22

         Set  h  =   createobject " Microsoft.XMLHTTP " )
23

        
' 向指定的URL发送Post消息
24

        h.open  " POST " " http://localhost/myService/test.asmx " False
25

        h.setRequestHeader 
" Content-Type " " text/xml"
26

              h.setRequestHeader  " SOAPAction " " http://tempuri.org/myService/test/isNumner"
27

        h.send (strxml)
28

        
While  h.readyState  <>   4
29

        
Wend
30

        
' 显示返回的XML信息
31

        str  =  h.responseText
32

        
' 将返回的XML信息解析并且显示返回值
33

         ' Set x = createobject("MSXML2.DOMDocument")
34

         '  x.loadXML str
35

        
36

        
' str = x.childNodes(1).Text
37

         
38

         response.write(str)
39

 
40

%
>
 3.在.net中 在.net中调用WebService就方便多了,没有必要自己写soap消息了,以上都是用XMLHTTP来发送WebService请求的,在.net只要添加了web引用,会自动为你创建一个代理类。然后使用代理类就像用自己定义的类一样方便。   在MSDN里也有: ms-help://MS.MSDNQTR.2003FEB.2052/behavior/workshop/author/webservice/reference/methods/callservice.htm 以下是userService和callService方法的详细说明:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐