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文件中。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值