BlogJava 首頁 新隨筆 聯系 聚合 管理
            37 Posts :: 64 Stories :: 21 Comments :: 0 Trackbacks

          1.  創建webservice,為了免于落俗我稍稍修改了創建webserice的默認webmethod。^_^
          using System;
          using System.Web;
          using System.Web.Services;
          using System.Web.Services.Protocols;

          [WebService(Namespace = " [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
          public class Service : System.Web.Services.WebService
          {
              public Service () {

                  //Uncomment the following line if using designed components
                  //InitializeComponent();
              }

              [WebMethod]
              public string SayHelloTo(string Name) {
                  return "Hello "+Name;
              }
             
          }
          還是俗了點。:)

          2. js調用webservice+xmlhttp的實現部分。

          <html>
          <title>
          Call webservice with javascript and xmlhttp.
          </title>
          <body>
          <script language="javascript">

          //Test function with get method.
          function RequestByGet(data){

          var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          //Webservice location.
          var URL="
          http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";
          xmlhttp.Open("GET",URL, false);
          xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
          xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
          xmlhttp.Send(data);
          var result = xmlhttp.status;
          //OK
          if(result==200) {
          document.write(xmlhttp.responseText);
          }
          xmlhttp = null;
          }

          //Test function with post method
          function RequestByPost(value)
          {
          var data;
          data = '<?xml version="1.0" encoding="utf-8"?>';
          data = data + '<soap:Envelope xmlns:xsi=";
          data = data + '<soap:Body>';
          data = data + '<SayHelloTo xmlns="
          ;
          data = data + '<Name>'+value+'</Name>';
          data = data + '</SayHelloTo>';
          data = data + '</soap:Body>';
          data = data + '</soap:Envelope>';

          var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          var URL="
          http://localhost:1323/WebSite6/Service.asmx";
          xmlhttp.Open("POST",URL, false);
          xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");
          xmlhttp.SetRequestHeader ("SOAPAction"," xmlhttp.Send(data);
          document.write( xmlhttp.responseText);

          }

          </Script>

          <input type="button" value="CallWebserviceByGet" onClick="RequestByGet(null)">
          <input type="button" value="CallWebserviceByPost" onClick="RequestByPost('Zach')">

          </body>
          </html>
          對于使用post方法需要發送的那堆東東可以在webservice的測試頁面中找到,自己拼湊加上對應的參數就可以。

          我發現用post方法的時候響應很慢,是因為用Post方法時發送的數據多的原因嗎?


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 江城| 安化县| 土默特右旗| 阜新| 武川县| 吉隆县| 民县| 淳化县| 颍上县| 织金县| 临汾市| 吉安市| 内江市| 陆川县| 从江县| 余庆县| 眉山市| 蒙阴县| 井冈山市| 邻水| 东阿县| 土默特左旗| 石门县| 金川县| 西平县| 顺义区| 萨迦县| 北川| 石门县| 琼中| 濮阳市| 加查县| 海安县| 施秉县| 广平县| 茶陵县| 云南省| 安西县| 吉林省| 疏勒县| 五寨县|