我们在编写asp代码的时候,大家都知道可以通过post或者get获得form表单的数据,那么我们如何直接获得其他页面上的数据呢?这就要借助xmlhttp协议了。xmlhttp是xmldom技术的一部分。
下面的代码就是一个很简单的例子,我们利用xmlhttp技术,把http://www.codetoad.com/站点首页的代码以xml的形式完全获取,并且在页面中输出。
<%
dim objxmlhttp, xml
set xml = server.createobject("microsoft.xmlhttp")
xml.open "get", "http://www.codetoad.com/", false
pull the data from the web page
xml.send
response.write "heres the html we now have in our xml object"
response.write "<br><br><br>"
response.write "<xmp>"
response.write xml.responsetext
response.write "</xmp>"
response.write "<br><br><br>"
response.write " now heres how the page looks:<br><br>"
response.write xml.responsetext
set xml = nothing
%>
下面是另一个实例
<%
dim objhttp , objxml , objxsl
set objhttp = server.createobject("microsoft.xmlhttp")
objhttp.open "get", "http://p.moreover.com/cgi-local/page?c=pop%20music%20reviews&o=xml", false
objhttp.send
set objxml = objhttp.responsexml
set objxsl=server.createobject("microsoft.xmldom")
objxsl.async=false
objxsl.load(server.mappath("style.xsl"))
if (objxsl.parseerror.errorcode = 0) then
response.write(objxml.transformnode(objxsl))
else
response.write "error: " & objxsl.parseerror.reason & " url:" & objxsl.url
end if
set objhttp = nothing
set objxml = nothing
set objxsl = nothing
%>
style.xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/tr/wd-xsl">
<xsl:template match="/">
<html>
<head>
<title>moreover…</title>
</head>
<body bgcolor="ffffff">
<div align="center">
<table bgcolor="ffffff" border="0" cellpadding="4" cellspacing="0" width="100%">
<xsl:for-each select="moreovernews/article">
<tr valign="middle">
<td align="left" bgcolor="ffffff">
<xsl:attribute name="href">
<xsl:value-of select="url"/>
</xsl:attribute>
<xsl:attribute name="target">
_blank
</xsl:attribute>
<xsl:value-of select="headline_text"/>
<xsl:attribute name="href">
<xsl:value-of select="document_url"/>
</xsl:attribute>
<xsl:attribute name="target">
_blank
</xsl:attribute>
<xsl:value-of select="source"/>
<xsl:attribute name="href">
<xsl:value-of select="access_registration"/>
</xsl:attribute>
<xsl:attribute name="target">
_blank
</xsl:attribute>
<xsl:value-of select="access_status"/>
<xsl:value-of select="harvest_time"/> gmt
</td>
</tr>
</xsl:for-each>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>