EMROOM EMROOM为你带来创造E世界明天的资本 设为首页
加入收藏
站长信箱
首页网络编程ASP代码下载网页设计服 务 器网络安全数 据 库SEO探索业界动态IT数码IT报价网站地图
当前位置:EMROOM--ASP调用WEBSERVICE文档
ASP调用WEBSERVICE文档
〖双击滚屏,单击停止〗〖放大字体〗〖缩小字体
 

ASP调用WEBSERVICE
----INDEX----
1. soap请求方式
2. post请求方式
3. SHOWALLNODE函数(关于节点各属性和数据显示)
---------------------
一.SOAP请求示例
下面是一个 SOAP 请求示例。所显示的占位符需要由实际值替换。
POST /WebService1/UserSignOn.asmx HTTP/1.1
Host: 192.100.100.81
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/LoginByAccount"


http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 
    http://tempuri.org/">
      string
      string
   

 


为了与WEBSERVICE交互,需要构造一个与上完全相同的SOAP请求:
<%
url = "http://192.100.100.81/WebService1/UserSignOn.asmx"

SoapRequest=""& _
"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)&">"& _
""& _

"http://tempuri.org/"&CHR(34)&">"& _
""&username&""& _
""&password&""& _
"
"& _

""& _
""

Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST",url,false
xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"
xmlhttp.setRequestHeader "HOST","192.100.100.81"
xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/LoginByAccount" ‘一定要与WEBSERVICE的命名空间相同,否则服务会拒绝
xmlhttp.Send(SoapRequest)
‘这样就利用XMLHTTP成功发送了与SOAP示例所符的SOAP请求.
‘检测一下是否成功:
Response.Write xmlhttp.Status&” ”
Response.Write xmlhttp.StatusText
Set xmlhttp = Nothing
%>
如果成功会显示200 ok,不成功会显示 500 内部服务器错误? Connection: keep-alive .
成功后就可以利用WEBSERVICE的响应,如下:
SOAP响应示例
下面是一个 SOAP 响应示例。所显示的占位符需要由实际值替换。
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length


http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 
    http://tempuri.org/">
      string
   

 


这是与刚才SOAP请求示例所对应的SOAP响应示例,在成功发送请求后,就可以查看该响应 :
If xmlhttp.Status = 200 Then

Set xmlDOC =server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
xmlStr = xmlDOC.xml
Set xmlDOC=nothing
xmlStr = Replace(xmlStr,"<","<")
xmlStr = Replace(xmlStr,">",">")
Response.write xmlStr
Else

Response.Write xmlhttp.Status&" "
Response.Write xmlhttp.StatusText

End if
请求正确则给出完整响应,请求不正确(如账号,密码不对)响应的内容就会信息不完整.
取出响应里的数据,如下:
If xmlhttp.Status = 200 Then

Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
Response.Write xmlDOC.documentElement.selectNodes("//LoginByAccountResult")(0).text ‘显示节点为LoginByAccountResult的数据(有编码则要解码)
Set xmlDOC = nothing

Else

Response.Write xmlhttp.Status&" "
Response.Write xmlhttp.StatusText


End if

显示某节点各个属性和数据的FUNCTION:

Function showallnode(rootname,myxmlDOC)'望大家不断完鄯 2005-1-9 writed by 844
if rootname<>"" then

set nodeobj=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"")'当前结点对像
nodeAttributelen=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"").attributes.length'当前结点属性数

returnstring=returnstring&"
节点名称:"&rootname

if nodeobj.text<>"" then
returnstring=returnstring&"
节点的文本:("&nodeobj.text&")"
end if

returnstring=returnstring&"
{
"

if nodeAttributelen<>0 then
returnstring=returnstring&"
属性数有  "&nodeAttributelen&" 个,分别是:"
end if

for i=0 to nodeAttributelen-1
returnstring=returnstring&"

  • "&nodeobj.attributes(i).Name&": "&nodeobj.getAttribute(nodeobj.attributes(i).Name)&"
  • "
    next

    if nodeobj.childNodes.Length<>0 then
    if nodeobj.hasChildNodes() and lcase(nodeobj.childNodes.item(0).nodeName)<>"#text"  then'是否有子节点
    set childnodeobj=nodeobj.childNodes
    childnodelen=nodeobj.childNodes.Length
    returnstring=returnstring&"

    有 "&childnodelen&" 个子节点;
    分别是: "
    for i=0 to childnodelen-1
    returnstring=returnstring&"

  • "&childnodeobj.item(i).nodeName&"
  • "
    next
    end if
    end if

    returnstring=returnstring&"
    }
    "
    response.write returnstring
    set nodeobj=nothing
    end if
    End Function
    可以这样用:
    If xmlhttp.Status = 200 Then

    Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
    xmlDOC.load(xmlhttp.responseXML)
    showallnode "LoginByAccountResponse",xmlDOC’调用SHOWALLNODE
    Set xmlDOC = nothing

    Else

    Response.Write xmlhttp.Status&" "
    Response.Write xmlhttp.StatusText

    End if

    二.POST请求示例
    HTTP POST
    下面是一个 HTTP POST 请求示例。所显示的占位符需要由实际值替换。
    POST /WebService1/UserSignOn.asmx/LoginByAccount HTTP/1.1
    Host: 192.100.100.81
    Content-Type: application/x-www-form-urlencoded
    Content-Length: length

    username=string&password=string
    构造POST请求:
    <%
    url = "http://192.100.100.81/WebService1/UserSignOn.asmx/LoginByAccount"

    SoapRequest="username="&username&"&password="&password

    Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP")
    xmlhttp.Open "POST",url,false
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"’注意
    xmlhttp.setRequestHeader "HOST","192.100.100.81"
    xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)

    xmlhttp.Send(SoapRequest)
    ‘这样就利用XMLHTTP成功发送了与HTTP POST示例所符的POST请求.
    ‘检测一下是否成功:
    Response.Write xmlhttp.Status&” ”
    Response.Write xmlhttp.StatusText
    Set xmlhttp = Nothing
    %>
    如果成功会显示200 ok,不成功会显示 500 内部服务器错误? Connection: keep-alive .
    成功后就可以利用WEBSERVICE的响应,如下:
    HTTP POST
    下面是一个 HTTP POST 响应示例。所显示的占位符需要由实际值替换。
    HTTP/1.1 200 OK
    Content-Type: text/xml; charset=utf-8
    Content-Length: length


    stringhttp://tempuri.org/">string>


    显示:
    If xmlhttp.Status = 200 Then

    Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
    xmlDOC.load(xmlhttp.responseXML)
    showallnode "string",xmlDOC'调用SHOWALLNODE
    Set xmlDOC = nothing

    Else

    Response.Write xmlhttp.Status&" "
    Response.Write xmlhttp.StatusText

    End if

    广告位招租,联系QQ:416346708
    热点文章
    推荐文章
    放大字体〗〖缩小字体〗 〖关闭窗口 来源:web,发布于:EMROOM ,已被阅读:
    搜索更多相关:
    GOOGLE:ASP调用WEBSERVICE文档
    BAIDU:ASP调用WEBSERVICE文档
     
    文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。
    如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
    编辑EMAIL:emroom@126.com,谢谢!