网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.浅谈session_onend的调试.
.使用ASP建立Http组件.
.利用XSL和ASP实现XML文档在线编辑.
.把数据插入到数据库的两种方法.
.如何读出空格.
.构建你的网站新闻自动发布系统之.
.XML技术上传文件.
.用RecordSet实现分页(by Daniel .
.remote script文档(转载自微软)(.
.用asp获取文件md5值.
.如何用JavaScript识别Netscape 6.
.修改Metabase.xml文件提升IIS性能.
.用两行代码在浏览器中实现文件上.
.数据分页方法新思路,速度非常快.
.利用XMLHTTP 从其他页面获取数据.
.ASP中怎么实现SQL数据库备份、恢.
.ASP 中健壮的页结构的异常处理.
.ASP实用技巧28则.
.已调试好的asp程序在VB中转换为组.
.从数据库中动态选取下拉列表的方.

ASP调用WEBSERVICE文档

发表日期:2005-1-10


----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"

<?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
%>
如果成功会显示200 ok,不成功会显示 500 内部服务器错误〿 Connection: keep-alive .
成功后就可以利用WEBSERVICE的响应,如下:
SOAP响应示例
下面是一个 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

显示某节点各个属性和数据的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&"<BR>节点名称:"&rootname

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

returnstring=returnstring&"<BR>{<BR>"

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

for i=0 to nodeAttributelen-1
returnstring=returnstring&"<li>"&nodeobj.attributes(i).Name&":&nbsp;"&nodeobj.getAttribute(nodeobj.attributes(i).Name)&" </li>"
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&"<BR><BR>有 "&childnodelen&" 个子节点;<BR>分别是: "
for i=0 to childnodelen-1
returnstring=returnstring&"<li>"&childnodeobj.item(i).nodeName&"</li>"
next
end if
end if

returnstring=returnstring&"<BR>}<BR>"
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&"&nbsp;"
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&”&nbsp;”
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

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</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&"&nbsp;"
Response.Write xmlhttp.StatusText

End if


以上是ASP用XMLHTTP组件发送SOAP请求,调用WEBSERVICE的方法,本人推荐在ASP环境下使用第一种方法,如果有更好的方法请联系本人mailto:lyq8442002@msn.com .使用HTTP GET的方式如果有中文会出问题,数据量又不大。用HTTP POST的方法感觉多此一举,其实上面的例子就是用POST的方式,只不过不是用POST的请求。用SOAP TOOLKIT要装软件,而且已没有后继版本。---全文完

上一篇:IIS6架设网站常见问题及症状举例答疑 人气:14024
下一篇:用Asp获取Dll加密新闻内容 人气:12255
浏览全部WEBSERVICE的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐