ASP调用.net的webservices的实现方法

 
一.请求过程
下面是一个 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"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<LoginByAccount xmlns="http://tempuri.org/">
<username>string</username>
<password>string</password>
</LoginByAccount>
</soap:Body>
</soap:Envelope>

为了与WEBSERVICE交互,需要构造一个与上完全相同的SOAP请求:
<%
url 
= "http://192.100.100.81/WebService1/UserSignOn.asmx"
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>"& _
"<LoginByAccount xmlns="&CHR(34)&"http://tempuri.org/"&CHR(34)&">"& _
"<username>"&username&"</username>"& _
"<password>"&password&"</password>"& _
"</LoginByAccount>"& _
"</soap:Body>"& _
"</soap:Envelope>"
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
&&nbsp;”
Response.Write xmlhttp.StatusText
Set xmlhttp 
= Nothing
%>
<%
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
%>
<%
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 响应示例。所显示的占位符需要由实际值替换。
HTTP/1.1 200 OK
Content
-Type: text/xml; charset=utf-8
Content
-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<LoginByAccountResponse xmlns="http://tempuri.org/">
<LoginByAccountResult>string</LoginByAccountResult>
</LoginByAccountResponse>
</soap:Body>
</soap:Envelope>

这是与刚才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,"<","&lt;")
   xmlStr 
= Replace(xmlStr,">","&gt;")
   Response.write xmlStr 
Else
   Response.Write xmlhttp.Status
&"&nbsp;"
   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
&"&nbsp;"
  Response.Write xmlhttp.StatusText
End 
If

我做的实例:
         '请求过程 
           url 
= "http://172.16.0.0/web/loginuser.asmx" 
         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>" & _ 
         
"<CheckUser xmlns=" & Chr(34& "http://tempuri.org/" & Chr(34& ">" & _ 
         
"<UserId>" & userid & "</UserId>" & _ 
         
"<PassWord>" & password & "</PassWord>" & _ 
         
"</CheckUser>" & _ 
         
"</soap:Body>" & _ 
         
"</soap:Envelope>" 
         Set xmlhttp 
= CreateObject("Msxml2.XMLHTTP"
         xmlhttp.Open 
"POST", url, False 
         xmlhttp.setRequestHeader 
"Content-Type""text/xml;charset=utf-8" 
         xmlhttp.setRequestHeader 
"HOST""localhost" 
         xmlhttp.setRequestHeader 
"Content-Length", Len(SoapRequest) 
         xmlhttp.setRequestHeader 
"SOAPAction""http://tempuri.org/CheckUser" 
         xmlhttp.Send (SoapRequest)
         
'Response.Write xmlhttp.Status &"&nbsp;" 
         'Response.Write xmlhttp.StatusText
            
            '响应过程 

         If xmlhttp.Status = 200 Then 
            Set xmlDOC 
= CreateObject("MSXML.DOMDocument"
            xmlDOC.Load (xmlhttp.responseXML) 
            
'Response.Write xmlDOC.documentElement.selectNodes("//CheckUserResult")(0).text 
            CheckUserCorrect = xmlDOC.documentElement.selectNodes("//CheckUserResult")(0).Text 
            Set xmlDOC 
= Nothing 
         Else 
            CheckUserCorrect 
= xmlhttp.Status & ";" & xmlhttp.StatusText & ";" 
         End If 
posted on 2007-12-08 22:09  巍巍边疆  阅读(470)  评论(1编辑  收藏  举报