asp中访问web service的方法和例子

如果要用ASP来调用WevService,就一定要使用SOAP Toolkit或者XMLHTTP, 使用SOAP Client需要专门安装SOAP Toolkit,这对客户端来说不具有通用性,因此我们就学习使用XML来进行对WebService的调用。


例子一:

Set objHTTP = Server.CreateObject("MSXML2.XMLHTTP")
Set xmlDOC =Server.CreateObject("Microsoft.XMLDOM")
strWebserviceURL = "http://192.168.0.1/WebServiceTest/Service1.asmx/add"
'设置参数及其值
strRequest = "x="&request.Form("a")&"&y="&request.Form("b")
'strRequest = "tbname=123"
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


Set docXSL = server.CreateObject("Microsoft.XMLDOM")
docXSL.async = False
docXSL.load(server.MapPath("test1.xsl"))
'response.write docXSL.xml

Set docTran = server.CreateObject("Microsoft.XMLDOM")
xmlDOC.transformNodeToObject docXSL,docTran
'response.write docTran.xml

xmlStr = docTran.xml
xmlStr = Replace(xmlStr,"<","<",1,-1,1)
xmlStr = Replace(xmlStr,">",">",1,-1,1)
response.write xmlStr


end if
例子二:

关键你要知道找个服务有那些方法,如下面的Lookup,然后就是参数,
你可以构造xml文档,然后post到web 服务的asmx上面
sXml = ...
...
oXmlHttp.Send(sXml)
最后就可以得到它的返回,应该是xml的,采用dom去解析xml文档,得到你想要的结果.


<%
strDomain = Request.Form("strDomain")
strUrl = "http://whois.aspsir.com/whois.asmx/Lookup?strDomain=" & strDomain & ""
Set oXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
oXmlHttp.Open "GET", strUrl, False
oXmlHttp.Send()
If oXmlHttp.readyState <> 4 Then Response.Write Err.Description
strResult = bytes2BSTR(oXmlHttp.responseBody)
If Instr(strResult,"No match")<>0 Then
Response.Write "域名[www." & strDomain & "]居然还没有被注册!"
Else
Response.Write "呵呵,来晚了一步!域名已经被注册~"
End If
Set oXmlHttp = Nothing

Function bytes2BSTR(vIn)
Dim strReturn
Dim I, ThisCharCode, NextCharCode
strReturn = ""
For I = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn, I, 1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn, I + 1, 1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
I = I + 1
End If
Next
bytes2BSTR = strReturn
End Function
%>


<%
Set objHTTP = Server.CreateObject("MSXML2.XMLHTTP")
Set xmlDOC =Server.CreateObject("MSXML.DOMDocument")
strWebserviceURL = "http://localhost/WebService1/Service1.asmx/Add"
'设置参数和值
strRequest = "a=5&b=6"
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&"
"
Response.Write objHTTP.StatusText
end if
%>
以上代码在本地测试都没有问题(在部署webservice的本地机器上测试的),然而把strWebserviceURL = "http://localhost/WebService1/Service1.asmx/Add"改为部署在其他机器上的WebService时,却出了问题,结果一直是返回500错误,即objHTTP.Status一直都为500。
  原因在于.Net Framework1.1默认不支持HttpGet和HttpPost。如果修改webservice里的web.config增加上代码5后,上代码就可以调用远程机器上的WebService了。 
 

你可能感兴趣的:(Web,xml,webservice,service,SOAP,asp)