加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

vbs调用WebService

发布时间:2020-12-17 02:58:55 所属栏目:安全 来源:网络整理
导读:最近碰到的一个问题,需要在asp和客户端调用.NET的webservice,也就是说需要用vbscript或javascript来调用webservice。在网上看了看,大多数方案都是利用SOAP Toolkit,但是因为SOAP Toolkit在今年就会被停止后续的支持了,并且要使用soapclient需要专门安装
最近碰到的一个问题,需要在asp和客户端调用.NET的webservice,也就是说需要用vbscript或javascript来调用webservice。在网上看了看,大多数方案都是利用SOAP Toolkit,但是因为SOAP Toolkit在今年就会被停止后续的支持了,并且要使用soapclient需要专门安装SOAP Toolkit,这对客户端来说不具有通用性,因此想到了使用xmlhttp,利用xmlhttp来和webservice交互。

客户端代码如下:
<script language="vbscript">
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Set xmlDOC =CreateObject("MSXML.DOMDocument")
strWebserviceURL = "
http://localhost/possible/Service1.asmx/add"
'设置参数及其值
strRequest = "x=2&y=3"
objHTTP.Open "POST",strWebserviceURL,False
'设置这个Content-Type很重要
objHTTP.SetRequestHeader "Content-Type","application/x-www-form-urlencoded"
objHTTP.Send(strRequest)
bOK = xmlDOC.load(objHTTP.responseXML)
'看看状态值
msgBox objHTTP.Status
msgbox objHTTP.StatusText
'objHTTP.Status=200,这里就可以处理返回的xml片段了
'如果需要,可以替换返回的xml字符串当中的&lt;和&gt;

xmlStr = xmlDOC.xml
xmlStr = Replace(xmlStr,"&lt;","<",1,-1,1)
xmlStr = Replace(xmlStr,"&gt;",">",1)
msgbox xmlStr
</script>

改为服务器端的asp代码为:
<%
Set objHTTP = Server.CreateObject("MSXML2.XMLHTTP")
Set xmlDOC =Server.CreateObject("MSXML.DOMDocument")
strWebserviceURL = "
http://localhost/possible/Service1.asmx/add"
'设置参数及其值
strRequest = "x=2&y=3"
objHTTP.Open "POST",False
'设置这个Content-Type很重要
objHTTP.SetRequestHeader "Content-Type","application/x-www-form-urlencoded"
objHTTP.Send(strRequest)
bOK = xmlDOC.load(objHTTP.responseXML)
'看看状态值
if objHTTP.Status=200 then
xmlStr = xmlDOC.xml
xmlStr = Replace(xmlStr,1)
? Response.Write xmlStr
else
? Response.Write objHTTP.Statu&"<br>"
? Response.Write objHTTP.StatusText
end if
%>

??? 以上代码在本地测试都没有问题(在部署webservice的本地机器上测试的),然而把strWebserviceURL = "http://localhost/possible/Service1.asmx/add"改为部署在其他机器上的webservice时,却出了问题,结果一直是返回500错误,即objHTTP.Status一直都为500。
??? 原因在于.Net Framework1.1默认不支持HttpGet和HttpPost。如果修改webservice里的web.config增加
?<webServices>
??????? ?<protocols>
??????????????? ?<add name="HttpPost"/>
??????????????? ?<add name="HttpGet"/>
??????????????? </protocols>
?</webServices>

后,上代码就可以调用远程机器上的webservice了。
??? 而利用SOAP发送在默认情况下即可得到.Net Framework1.1的支持,因此用构造Soap请求的xml字符串给xmlhttp对象来send的方法就对远程服务器的web.config没有要求了,于是根据local显示的例子构造了一个soapRequest的string,发送给了即将部署的远程主机,结果返回了200的status code,并且可以顺利取得responseXML.类似代码如下:

客户端代码如下:
<script language="vbscript">
Dim url,xmlhttp,dom,node,xmlDOC
'根据webservice的测试页不同的方法构造不同的soap request
SoapRequest = "<?xml version="&CHR(34)&"1.0"&CHR(34)&" encoding="&CHR(34)&"utf-8"&CHR(34)&"?>"& _
??? "<soap:Envelope xmlns:xsi="&CHR(34)&"
http://www.w3.org/2001/XMLSchema-instance"&CHR(34)&" "& _
??? "xmlns:xsd="&CHR(34)&"
http://www.w3.org/2001/XMLSchema"&CHR(34)&" "& _
??? "xmlns:soap="&CHR(34)&"
http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _
??? "<soap:Body>"& _
??? "<add xmlns="&CHR(34)&"
http://localhost"&CHR(34)&">"& _
???? "<x>3</x>"& _
???? "<y>4</y>"& _
??? "</add>"& _
???? "</soap:Body>"& _
?? "</soap:Envelope>"
url = "
http://www.xxxx.com/Service1.asmx?methodname=Add"
Set xmlDOC =CreateObject("MSXML.DOMDocument")
xmlDOC.loadXML(SoapRequest)
Set xmlhttp = CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST",url,false
xmlhttp.setRequestHeader "Content-Type","text/xml;charset=utf-8"
'SOAPAction这个Header头同样可以在sample中找到
xmlhttp.setRequestHeader "SOAPAction","
http://localhost/add"
xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
xmlhttp.Send(xmlDOC)
msgbox xmlhttp.Status
msgbox xmlhttp.StatusText
msgbox xmlhttp.responseText
If xmlhttp.Status = 200 Then
?xmlDOC.load(xmlhttp.responseXML)
?msgbox "执行结果为:"&xmlDOC.getElementsByTagName("addResult")(0).text
else
?msgbox "failed"
end if
</script>

改为服务器端的asp代码为:
<%
Dim url,xmlDOC
'根据webservice的测试页不同的方法构造不同的soap request
SoapRequest = "<?xml version="&CHR(34)&"1.0"&CHR(34)&" encoding="&CHR(34)&"utf-8"&CHR(34)&"?>"& _
??? "<soap:Envelope xmlns:xsi="&CHR(34)&"
http://www.w3.org/2001/XMLSchema-instance"&CHR(34)&" "& _
??? "xmlns:xsd="&CHR(34)&"
http://www.w3.org/2001/XMLSchema"&CHR(34)&" "& _
??? "xmlns:soap="&CHR(34)&"
http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _
??? "<soap:Body>"& _
??? "<add xmlns="&CHR(34)&"
http://localhost"&CHR(34)&">"& _
???? "<x>3</x>"& _
???? "<y>4</y>"& _
??? "</add>"& _
???? "</soap:Body>"& _
?? "</soap:Envelope>"
url = "
http://www.xxxx.com/Service1.asmx?methodname=Add"
Set xmlDOC =server.CreateObject("MSXML.DOMDocument")
xmlDOC.loadXML(SoapRequest)
Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST","text/xml;charset=utf-8"
xmlhttp.setRequestHeader "SOAPAction",LEN(SoapRequest)
xmlhttp.Send(xmlDOC)
If xmlhttp.Status = 200 Then
?xmlDOC.load(xmlhttp.responseXML)
? Response.Write xmlhttp.Status&"<br>"
? Response.Write xmlhttp.StatusText&"<br>执行结果为:"
?Response.Write xmlDOC.getElementsByTagName("addResult")(0).text
else
? Response.Write xmlhttp.Status&"<br>"
? Response.Write xmlhttp.StatusText
end if
%>

以上用的都是vbscript的,对于javascript基本上都是一样的,只需要做一些小的改动,具体代码这里就省略了。

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

附:
测试时用的webservice文件Service1.asmx的代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace possible
{
?/// <summary>
?/// Service1 的摘要说明。
?/// </summary>
?[WebService(Description="my web service",Name="myService",Namespace="
http://localhost")]
?public class myService : System.Web.Services.WebService
?{
??public myService()
??{
???//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
???InitializeComponent();
??}

??#region 组件设计器生成的代码
??
??//Web 服务设计器所必需的
??private IContainer components = null;
????
??/// <summary>
??/// 设计器支持所需的方法 - 不要使用代码编辑器修改
??/// 此方法的内容。
??/// </summary>
??private void InitializeComponent()
??{
??}

??/// <summary>
??/// 清理所有正在使用的资源。
??/// </summary>
??protected override void Dispose( bool disposing )
??{
???if(disposing && components != null)
???{
????components.Dispose();
???}
???base.Dispose(disposing);??
??}
??
??#endregion

??[WebMethod(Description="返回两整数之和")]??public int add(int x,int y)??{???return x+y;??}?}}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读