vbs呼び出しWebService--xmlhttpを使用

3707 ワード

具体的に呼び出されたコード:
'          ,           

Function Z_WriteFile(sFileName, sText, bAppend)

    Dim fs, fso, iomode

    if bAppend = True Then

        iomode = 8              'ForAppending

    else

        iomode = 2              'ForWriting

    end if



    set fs = CreateObject("Scripting.FileSystemObject")

    set fso = fs.OpenTextFile(sFileName, iomode, True)  '            ,     

    fso.WriteLine sText

    fso.Close



    set fso = Nothing

    set fs = Nothing

    Z_WriteFile = True

End Function



Dim objHttp, xmlDoc, sText, sXml

Set objHTTP = CreateObject("MSXML2.XMLHTTP")

Set xmlDOC = CreateObject("MSXML.DOMDocument")

strWebserviceURL = "http://192.168.2.39/webservice1/service.asmx/addition"

strRequest = "i=2&j=3"

objHTTP.Open "POST", strWebserviceURL, False

objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"

objHTTP.Send(strRequest)



if objHTTP.Status = 200 Then

    Dim sNodeList, sResult

    xmlDOC.load(objHTTP.responseXML)

    set sNodeList = xmlDoc.getElementsByTagName("double")

    sResult = sNodeList(0).Text

    sText = "2+3=" & sResult

else

    sText = "  WebService  ,   "

end if



Z_WriteFile "ResultVoice.txt", sText, False


Webserviceはvs.net 2010(c#)によって開発され、コアコードは以下の通りである.
using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;



[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService

{

    public Service () {



        //         ,         

        //InitializeComponent(); 

    }



    [WebMethod(Description = "Let's say \"Hello\"")]

    public string Hi()

    {

        return "Hello World, Happy New Year";

    }



    [WebMethod(Description = "Hello JoeBlack")]

    public string Hello(string username)

    {

        return username + ", Happy New Year";

    }



    [WebMethod(Description = "     ")]

    public double addition(double i, double j)

    {

        return i + j;

    }



    [WebMethod(Description = "     ")]

    public double subtract(double i, double j)

    {

        return i - j;

    }



    [WebMethod(Description = "     ")]

    public double multiply(double i, double j)

    {

        return i * j;

    }



    [WebMethod(Description = "     ")]

    public double division(double i, double j)

    {

        if (j != 0)

            return i / j;

        else

            return 0;

    }

    

}


WebServiceがHttpGetおよびHttpPostプロトコルのサポートを有効にするには(デフォルトではSoapプロトコルのみがサポートされています)、WebServiceプロジェクトのWeb.configに次の内容を追加する必要があります.
<system.web>

		<!--

               compilation debug="true"       

                     。

                     ,             

                true。

        -->

		<compilation debug="true" targetFramework="4.0">

		</compilation>

		<!--

             <authentication>      

                  ,ASP.NET 

                        。 

        -->

    

    <!--

            WebService  HttpPost HttpGet  

    -->

    <webServices>

      <protocols>

        <add name="HttpPost" />

        <add name="HttpGet" />

      </protocols>

    </webServices>

    

		<authentication mode="Windows"/>


実行すると、処理後の結果がResultVoice.txtファイルに書き込まれます.