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文件中。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)