• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當(dāng)前位置:雨林木風(fēng)下載站 > 技術(shù)開(kāi)發(fā)教程 > 詳細(xì)頁(yè)面

        運(yùn)用vbscript腳本調(diào)用web服務(wù)

        運(yùn)用vbscript腳本調(diào)用web服務(wù)

        更新時(shí)間:2022-05-06 文章作者:未知 信息來(lái)源:網(wǎng)絡(luò) 閱讀次數(shù):

        最近碰到的一個(gè)問(wèn)題,需要在asp和客戶端調(diào)用.NET的webservice,也就是說(shuō)需要用vbscript或javascript來(lái)調(diào)用webservice。在網(wǎng)上看了看,大多數(shù)方案都是利用SOAP Toolkit,但是因?yàn)镾OAP Toolkit在今年就會(huì)被停止后續(xù)的支持了,并且要使用soapclient需要專門(mén)安裝SOAP Toolkit,這對(duì)客戶端來(lái)說(shuō)不具有通用性,因此想到了使用xmlhttp,利用xmlhttp來(lái)和webservice交互。

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

        改為服務(wù)器端的asp代碼為:
        <%
        Set objHTTP = Server.CreateObject("MSXML2.XMLHTTP")
        Set xmlDOC =Server.CreateObject("MSXML.DOMDocument")
        strWebserviceURL = "http://localhost/possible/Service1.asmx/add"
        '設(shè)置參數(shù)及其值
        strRequest = "x=2&y=3"
        objHTTP.Open "POST", strWebserviceURL, False
        '設(shè)置這個(gè)Content-Type很重要
        objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        objHTTP.Send(strRequest)
        bOK = xmlDOC.load(objHTTP.responseXML)
        '看看狀態(tài)值
        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
        %>

        以上代碼在本地測(cè)試都沒(méi)有問(wèn)題(在部署webservice的本地機(jī)器上測(cè)試的),然而把strWebserviceURL = "http://localhost/possible/Service1.asmx/add"改為部署在其他機(jī)器上的webservice時(shí),卻出了問(wèn)題,結(jié)果一直是返回500錯(cuò)誤,即objHTTP.Status一直都為500。
        原因在于.Net Framework1.1默認(rèn)不支持HttpGet和HttpPost。如果修改webservice里的web.config增加
        <webServices>
        <protocols>
        <add name="HttpPost"/>
        <add name="HttpGet"/>
        </protocols>
        </webServices>
        后,上代碼就可以調(diào)用遠(yuǎn)程機(jī)器上的webservice了。
        而利用SOAP發(fā)送在默認(rèn)情況下即可得到.Net Framework1.1的支持,因此用構(gòu)造Soap請(qǐng)求的xml字符串給xmlhttp對(duì)象來(lái)send的方法就對(duì)遠(yuǎn)程服務(wù)器的web.config沒(méi)有要求了,于是根據(jù)local顯示的例子構(gòu)造了一個(gè)soapRequest的string,發(fā)送給了即將部署的遠(yuǎn)程主機(jī),結(jié)果返回了200的status code,并且可以順利取得responseXML.類(lèi)似代碼如下:

        客戶端代碼如下:
        <script language="vbscript">
        Dim url,xmlhttp,dom,node,xmlDOC
        '根據(jù)webservice的測(cè)試頁(yè)不同的方法構(gòu)造不同的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這個(gè)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 "執(zhí)行結(jié)果為:"&xmlDOC.getElementsByTagName("addResult")(0).text
        else
        msgbox "failed"
        end if
        </script>

        改為服務(wù)器端的asp代碼為:
        <%
        Dim url,xmlhttp,dom,node,xmlDOC
        '根據(jù)webservice的測(cè)試頁(yè)不同的方法構(gòu)造不同的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>執(zhí)行結(jié)果為:"
        Response.Write xmlDOC.getElementsByTagName("addResult")(0).text
        else
        Response.Write xmlhttp.Status&"<br>"
        Response.Write xmlhttp.StatusText
        end if
        %>

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

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

        附:
        測(cè)試時(shí)用的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 的摘要說(shuō)明。
        /// </summary>
        [WebService(Description="my web service",Name="myService",Namespace="http://localhost")]
        public class myService : System.Web.Services.WebService
        {
        public myService()
        {
        //CODEGEN: 該調(diào)用是 ASP.NET Web 服務(wù)設(shè)計(jì)器所必需的
        InitializeComponent();
        }

        #region 組件設(shè)計(jì)器生成的代碼

        //Web 服務(wù)設(shè)計(jì)器所必需的
        private IContainer components = null;

        /// <summary>
        /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
        /// 此方法的內(nèi)容。
        /// </summary>
        private void InitializeComponent()
        {
        }

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

        #endregion

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

        溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!

        本類(lèi)教程下載

        系統(tǒng)下載排行

        主站蜘蛛池模板: 亚洲人成网站免费播放| 高清在线亚洲精品国产二区| 国产免费久久精品99re丫y| 婷婷亚洲综合五月天小说| 久久亚洲AV成人无码国产最大| a成人毛片免费观看| 免费A级毛片无码视频| 亚洲成av人片不卡无码久久| 亚洲国产精品免费在线观看| 91视频免费网站| 亚洲国产精品无码久久一区二区| 国产精品成人亚洲| 37pao成人国产永久免费视频| 相泽亚洲一区中文字幕| 色偷偷亚洲第一综合网| 无码中文在线二区免费| 亚洲国产无线乱码在线观看 | 亚洲日韩精品无码AV海量| 久久青青草原国产精品免费| 亚洲精品黄色视频在线观看免费资源| 亚洲国产精品xo在线观看| 国产精品免费无遮挡无码永久视频| 亚洲视频在线精品| 日韩亚洲综合精品国产| 亚洲一区二区三区无码中文字幕| 人体大胆做受免费视频| 免费国产一级特黄久久| 国产精品无码亚洲精品2021| 中文字幕在线亚洲精品 | 免费一看一级毛片| 国产精品免费在线播放| 亚洲美日韩Av中文字幕无码久久久妻妇| 成人毛片100免费观看| 亚洲中文字幕无码一区| 亚洲视频在线免费播放| 一区二区三区AV高清免费波多| 亚洲人成在线观看| 免费成人福利视频| 亚洲一区二区三区在线| 黄色成人网站免费无码av| 亚洲欧美日韩一区二区三区 |