精彩的人生

          好好工作,好好生活

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            147 Posts :: 0 Stories :: 250 Comments :: 0 Trackbacks
          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引用,會自動為你創建一個代理類。然后使用代理類就像用自己定義的類一樣方便。


          原文地址:http://ewebapp.cnblogs.com/articles/237386.html
          posted on 2006-05-07 15:12 hopeshared 閱讀(618) 評論(0)  編輯  收藏 所屬分類: Web Service
          主站蜘蛛池模板: 阿荣旗| 理塘县| 老河口市| 陕西省| 怀集县| 民和| 铜山县| 惠水县| 永年县| 黄浦区| 乌海市| 密山市| 海城市| 保定市| 福泉市| 天全县| 四川省| 通化市| 积石山| 宜城市| 安吉县| 阿图什市| 来凤县| 台安县| 汝城县| 双辽市| 会东县| 台北县| 海门市| 宁明县| 巢湖市| 信宜市| 陕西省| 德令哈市| 永清县| 任丘市| 乌拉特后旗| 资兴市| 长汀县| 岱山县| 昔阳县|