幾種調用WebService的方法
2024-07-21 02:21:22
供稿:網友
 
1. 在javascript中調用webservice
<script language="javascript">
function postrequestdata(url,data){
 var xmlhttp = new activexobject("microsoft.xmlhttp");
 xmlhttp.open("post",url, false);
 xmlhttp.setrequestheader ("content-type","text/xml; charset=utf-8");
 xmlhttp.setrequestheader ("soapaction","http://tempuri.org/myservice/test/isnumner");
 
 try { 
 xmlhttp.send(data); 
 var result = xmlhttp.status;
 }
 catch(ex) {
 return("0" + ex.description + "|" + ex.number); 
 }
 if(result==200) { 
 return("1" + xmlhttp.responsetext); 
 }
 xmlhttp = null;
}
 
function loadit(value){
 var url = 'http://localhost/myservice/test.asmx';
 var data ;
 var r;
 
 data = '<?xml version="1.0" encoding="utf-8"?>';
 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/">';
 data = data + '<soap:body>';
 data = data + '<isnumner xmlns="http://tempuri.org/myservice/test">';
 data = data + '<str>'+value+'</str>';
 data = data + '</isnumner>';
 data = data + '</soap:body>';
 data = data + '</soap:envelope>';
 
 r=postrequestdata(url,data);
 document.write(r); 
}
loadit('5');
</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中
<%@language="vbscript" codepage="936"%>
<%
 dim strxml 
 dim str 
 
 '定義soap消息
 strxml = "<?xml version='1.0' encoding='tf-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/'>"
 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>"
 
 '定義一個xml的文檔對象,將手寫的或者接受的xml內容轉換成xml對象
 'set x = createobject("microsoft.domdocument")
 '初始化xml對象
 '將手寫的soap字符串轉換為xml對象
 ' x.loadxml strxml
 '初始化http對象
 set h = createobject( "microsoft.xmlhttp")
 '向指定的url發送post消息
 h.open "post", "http://localhost/myservice/test.asmx", false
 h.setrequestheader "content-type", "text/xml"
 h.setrequestheader "soapaction", "http://tempuri.org/myservice/test/isnumner"
 h.send (strxml)
 while h.readystate <> 4
 wend
 '顯示返回的xml信息
 str = h.responsetext
 '將返回的xml信息解析并且顯示返回值
 'set x = createobject("msxml2.domdocument")
 ' x.loadxml str
 
 'str = x.childnodes(1).text
 
 response.write(str)
 
%>
 3.在.net中
在.net中調用webservice就方便多了,沒有必要自己寫soap消息了,以上都是用xmlhttp來發送webservice請求的,在.net只要添加了web引用,會自動為你創建一個代理類。然后使用代理類就像用自己定義的類一樣方便。