<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    happyfish

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      38 Posts :: 1 Stories :: 62 Comments :: 0 Trackbacks

      最近碰到的一個問題,需要在asp和客戶端調用.NET的webservice,也就是說需要用vbscript或javascript來調用webservice。在網上看了看,大多數方案都是利用SOAP Toolkit,但是因為SOAP Toolkit在今年就會被停止后續的支持了,并且要使用soapclient需要專門安裝SOAP Toolkit,這對客戶端來說不具有通用性,因此想到了使用xmlhttp,利用xmlhttp來和webservice交互。

    客戶端代碼如下:
    <script language="vbscript">
    Set objHTTP = CreateObject("MSXML2.XMLHTTP")
    Set xmlDOC =CreateObject("MSXML.DOMDocument")
    strWebserviceURL = "
    http://localhost/possible/Service1.asmx/add"
    '設置參數及其值
    strRequest = "x=2&y=3"
    objHTTP.Open "POST", strWebserviceURL, False
    '設置這個Content-Type很重要
    objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    objHTTP.Send(strRequest)
    bOK = xmlDOC.load(objHTTP.responseXML)
    '看看狀態值
    msgBox objHTTP.Status
    msgbox objHTTP.StatusText
    'objHTTP.Status=200,這里就可以處理返回的xml片段了
    '如果需要,可以替換返回的xml字符串當中的&lt;和&gt;

    xmlStr = xmlDOC.xml
    xmlStr = Replace(xmlStr,"&lt;","<",1,-1,1)
    xmlStr = Replace(xmlStr,"&gt;",">",1,-1,1)
    msgbox xmlStr
    </script>

    改為服務器端的asp代碼為:
    <%
    Set objHTTP = Server.CreateObject("MSXML2.XMLHTTP")
    Set xmlDOC =Server.CreateObject("MSXML.DOMDocument")
    strWebserviceURL = "
    http://localhost/possible/Service1.asmx/add"
    '設置參數及其值
    strRequest = "x=2&y=3"
    objHTTP.Open "POST", strWebserviceURL, False
    '設置這個Content-Type很重要
    objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    objHTTP.Send(strRequest)
    bOK = xmlDOC.load(objHTTP.responseXML)
    '看看狀態值
    if objHTTP.Status=200 then
    xmlStr = xmlDOC.xml
    xmlStr = Replace(xmlStr,"&lt;","<",1,-1,1)
    xmlStr = Replace(xmlStr,"&gt;",">",1,-1,1)
      Response.Write xmlStr
    else
      Response.Write objHTTP.Statu&"<br>"
      Response.Write objHTTP.StatusText
    end if
    %>

        以上代碼在本地測試都沒有問題(在部署webservice的本地機器上測試的),然而把strWebserviceURL = "http://localhost/possible/Service1.asmx/add"改為部署在其他機器上的webservice時,卻出了問題,結果一直是返回500錯誤,即objHTTP.Status一直都為500。
        原因在于.Net Framework1.1默認不支持HttpGet和HttpPost。如果修改webservice里的web.config增加
     <webServices>
             <protocols>
                     <add name="HttpPost"/>
                     <add name="HttpGet"/>
                    </protocols>
     </webServices>

    后,上代碼就可以調用遠程機器上的webservice了。
        而利用SOAP發送在默認情況下即可得到.Net Framework1.1的支持,因此用構造Soap請求的xml字符串給xmlhttp對象來send的方法就對遠程服務器的web.config沒有要求了,于是根據local顯示的例子構造了一個soapRequest的string,發送給了即將部署的遠程主機,結果返回了200的status code,并且可以順利取得responseXML.類似代碼如下:

    客戶端代碼如下:
    <script language="vbscript">
    Dim url,xmlhttp,dom,node,xmlDOC
    '根據webservice的測試頁不同的方法構造不同的soap request
    SoapRequest = "<?xml version="&CHR(34)&"1.0"&CHR(34)&" encoding="&CHR(34)&"utf-8"&CHR(34)&"?>"& _
        "<soap:Envelope xmlns:xsi="&CHR(34)&"
    http://www.w3.org/2001/XMLSchema-instance"&CHR(34)&" "& _
        "xmlns:xsd="&CHR(34)&"
    http://www.w3.org/2001/XMLSchema"&CHR(34)&" "& _
        "xmlns:soap="&CHR(34)&"
    http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _
        "<soap:Body>"& _
        "<add xmlns="&CHR(34)&"
    http://localhost"&CHR(34)&">"& _
         "<x>3</x>"& _
         "<y>4</y>"& _
        "</add>"& _
         "</soap:Body>"& _
       "</soap:Envelope>"
    url = "
    http://www.xxxx.com/Service1.asmx?methodname=Add"
    Set xmlDOC =CreateObject("MSXML.DOMDocument")
    xmlDOC.loadXML(SoapRequest)
    Set xmlhttp = CreateObject("Msxml2.XMLHTTP")
    xmlhttp.Open "POST",url,false
    xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"
    'SOAPAction這個Header頭同樣可以在sample中找到
    xmlhttp.setRequestHeader "SOAPAction", "
    http://localhost/add"
    xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
    xmlhttp.Send(xmlDOC)
    msgbox xmlhttp.Status
    msgbox xmlhttp.StatusText
    msgbox xmlhttp.responseText
    If xmlhttp.Status = 200 Then
     xmlDOC.load(xmlhttp.responseXML)
     msgbox "執行結果為:"&xmlDOC.getElementsByTagName("addResult")(0).text
    else
     msgbox "failed"
    end if
    </script>

    改為服務器端的asp代碼為:
    <%
    Dim url,xmlhttp,dom,node,xmlDOC
    '根據webservice的測試頁不同的方法構造不同的soap request
    SoapRequest = "<?xml version="&CHR(34)&"1.0"&CHR(34)&" encoding="&CHR(34)&"utf-8"&CHR(34)&"?>"& _
        "<soap:Envelope xmlns:xsi="&CHR(34)&"
    http://www.w3.org/2001/XMLSchema-instance"&CHR(34)&" "& _
        "xmlns:xsd="&CHR(34)&"
    http://www.w3.org/2001/XMLSchema"&CHR(34)&" "& _
        "xmlns:soap="&CHR(34)&"
    http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _
        "<soap:Body>"& _
        "<add xmlns="&CHR(34)&"
    http://localhost"&CHR(34)&">"& _
         "<x>3</x>"& _
         "<y>4</y>"& _
        "</add>"& _
         "</soap:Body>"& _
       "</soap:Envelope>"
    url = "
    http://www.xxxx.com/Service1.asmx?methodname=Add"
    Set xmlDOC =server.CreateObject("MSXML.DOMDocument")
    xmlDOC.loadXML(SoapRequest)
    Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP")
    xmlhttp.Open "POST",url,false
    xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"
    xmlhttp.setRequestHeader "SOAPAction", "
    http://localhost/add"
    xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
    xmlhttp.Send(xmlDOC)
    If xmlhttp.Status = 200 Then
     xmlDOC.load(xmlhttp.responseXML)
      Response.Write xmlhttp.Status&"<br>"
      Response.Write xmlhttp.StatusText&"<br>執行結果為:"
     Response.Write xmlDOC.getElementsByTagName("addResult")(0).text
    else
      Response.Write xmlhttp.Status&"<br>"
      Response.Write xmlhttp.StatusText
    end if
    %>

    以上用的都是vbscript的,對于javascript基本上都是一樣的,只需要做一些小的改動,具體代碼這里就省略了。

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    附:
    測試時用的webservice文件Service1.asmx的代碼:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;

    namespace possible
    {
     /// <summary>
     /// Service1 的摘要說明。
     /// </summary>
     [WebService(Description="my web service",Name="myService",Namespace="
    http://localhost")]
     public class myService : System.Web.Services.WebService
     {
      public myService()
      {
       //CODEGEN: 該調用是 ASP.NET Web 服務設計器所必需的
       InitializeComponent();
      }

      #region 組件設計器生成的代碼
      
      //Web 服務設計器所必需的
      private IContainer components = null;
        
      /// <summary>
      /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
      /// 此方法的內容。
      /// </summary>
      private void InitializeComponent()
      {
      }

      /// <summary>
      /// 清理所有正在使用的資源。
      /// </summary>
      protected override void Dispose( bool disposing )
      {
       if(disposing && components != null)
       {
        components.Dispose();
       }
       base.Dispose(disposing);  
      }
      
      #endregion

      [WebMethod(Description="返回兩整數之和")]
      public int add(int x,int y)
      {
       return x+y;
      }
     }
    }

    posted on 2005-03-30 14:40 小魚兒 閱讀(151) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 很黄很色很刺激的视频免费| 久久99青青精品免费观看| 成人免费视频观看无遮挡| 久久精品a亚洲国产v高清不卡| 皇色在线免费视频| 亚洲日韩国产成网在线观看| 日韩精品视频在线观看免费| 亚洲国产成人影院播放| 国产成人自产拍免费视频| 亚洲色大成网站WWW久久九九| 中国在线观看免费的www| 亚洲国产成人私人影院| 67194国产精品免费观看| 亚洲av产在线精品亚洲第一站| 国产在线观看片a免费观看| 亚洲av永久无码精品三区在线4| 成年女人毛片免费播放视频m| 日本亚洲欧美色视频在线播放 | 久久久久无码精品亚洲日韩| 久久精品免费观看国产| 亚洲一区二区三区亚瑟| 国产国产成年年人免费看片| 国产精品黄页免费高清在线观看| 亚洲第一精品福利| 成人网站免费观看| 一级一片免费视频播放| 精品亚洲成a人片在线观看少妇| 97视频热人人精品免费| 一级毛片免费在线观看网站| 亚洲av无码成人黄网站在线观看| av无码免费一区二区三区| 人妻无码中文字幕免费视频蜜桃 | 久久精品a一国产成人免费网站| 亚洲欧美日韩一区二区三区在线| 亚洲精品网站在线观看不卡无广告 | 亚洲w码欧洲s码免费| 久久精品国产亚洲AV未满十八| 亚洲男同帅GAY片在线观看| 成人奭片免费观看| 日本一区午夜艳熟免费| 中文有码亚洲制服av片|