具体调用的代码:
'向指定的文件写入文本, 并指定是否是添加内容
Function Z_WriteFile(sFileName, sText, bAppend)Dim fs, fso, iomodeif bAppend = True Theniomode = 8 'ForAppendingelseiomode = 2 'ForWritingend ifset fs = CreateObject("Scripting.FileSystemObject")set fso = fs.OpenTextFile(sFileName, iomode, True) '第三个参数表明文件不存在,则新建文件fso.WriteLine sTextfso.Closeset fso = Nothingset fs = NothingZ_WriteFile = True
End FunctionDim 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 ThenDim sNodeList, sResultxmlDOC.load(objHTTP.responseXML)set sNodeList = xmlDoc.getElementsByTagName("double")sResult = sNodeList(0).TextsText = "2+3=" & sResult
elsesText = "调用WebService出错,请检查"
end ifZ_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;elsereturn 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文件中。