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方法時發送的數據多的原因嗎?


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


          網站導航:
           
          主站蜘蛛池模板: 津市市| 荔波县| 灵寿县| 阿坝县| 中阳县| 中西区| 建水县| 长垣县| 梅州市| 化州市| 独山县| 成都市| 屏边| 女性| 高陵县| 陆河县| 尉氏县| 林口县| 济源市| 遂平县| 巴林左旗| 韩城市| 万载县| 姚安县| 舒兰市| 宾川县| 乡宁县| 蓝山县| 蒙自县| 车致| 庆安县| 宜都市| 县级市| 喀喇沁旗| 广水市| 喜德县| 盐边县| 九寨沟县| 德格县| 灵山县| 鹤山市|