• vbs调用WebService 使用xmlhttp


    具体调用的代码:

    '向指定的文件写入文本, 并指定是否是添加内容
    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.net2010(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文件中。

  • 相关阅读:
    关于获取路径
    今天最好的生日礼物就是重新找到目标
    Fedora与Ubuntu安装g++的命令
    CMPXCHG8B 比较并交换 8 字节
    关于 WIN32_LEAN_AND_MEAN
    i386和i686
    Intrinsic function
    VC9: LINK : warning LNK4068: /MACHINE not specified; defaulting to X86
    Linux内核中的Min和Max函数
    linux重定向命令应用及语法
  • 原文地址:https://www.cnblogs.com/joeblackzqq/p/1967748.html
Copyright © 2020-2023  润新知