最近碰到的一个问题,需要在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字符串当中的<和>
xmlStr = xmlDOC.xml
xmlStr = Replace(xmlStr,"<","<",1,-1,1)
xmlStr = Replace(xmlStr,">",">",1,-1,1)
msgbox xmlStr
</script>
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字符串当中的<和>
xmlStr = xmlDOC.xml
xmlStr = Replace(xmlStr,"<","<",1,-1,1)
xmlStr = Replace(xmlStr,">",">",1,-1,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", strWebserviceURL, 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,-1,1)
xmlStr = Replace(xmlStr,">",">",1,-1,1)
Response.Write xmlStr
else
Response.Write objHTTP.Statu&"<br>"
Response.Write objHTTP.StatusText
end if
%>
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", strWebserviceURL, 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,-1,1)
xmlStr = Replace(xmlStr,">",">",1,-1,1)
Response.Write xmlStr
else
Response.Write objHTTP.Statu&"<br>"
Response.Write objHTTP.StatusText
end if
%>
