vbs调用WebService--使用xmlhttp
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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是由2010(c#)开发的,核⼼代码如下:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "/")]
[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> 节可以配置
安全⾝份验证模式,
使⽤该模式来识别来访⽤户⾝份。
-->
<!--
配置WebService⽀持HttpPost和HttpGet协议
-->
<webServices>
<protocols>
<add name="HttpPost" />
<add name="HttpGet" />
</protocols>
</webServices>
<authentication mode="Windows"/>
运⾏下就可以看到处理后的结果,都写⼊到了ResultVoice.txt⽂件中。