利用XMLHTTP从其他页面获取数据
我们在编写ASP代码的时候,大家都知道可以通过post或者get获得form表单的数据,那么我们如何直接获得其他页面上的数据呢?这就要借助xmlhttp协议了。xmlhttp是xmldom技术的一部分。
下面的代码就是一个很简单的例子,我们利用xmlhttp技术,把http://www.xxxx.com/站点首页的代码以xml的形式完全获取,并且在页面中输出。
<%
DimobjXMLHTTP,xml
Setxml=Server.CreateObject("Microsoft.XMLHTTP")
xml.Open"GET","http://www.codetoad.com/",False
'Pullthedatafromthewebpage
xml.Send
Response.write"Here'sthehtmlwenowhaveinourxmlobject"
Response.write"<BR><BR><BR>"
Response.Write"<xmp>"
Response.Writexml.responseText
Response.Write"</xmp>"
Response.write"<BR><BR><BR>"
Response.write"Nowhere'showthepagelooks:<BR><BR>"
Response.Writexml.responseText
Setxml=Nothing
%>
下面是另一个实例
<%
dimobjHTTP,objXML,objXSL
setobjHTTP=Server.CreateObject("Microsoft.XMLHTTP")
objHTTP.open"GET","http://p.moreover.com/cgi-local/page?c=Pop%20music%20reviews&o=xml",false
objHTTP.send
setobjXML=objHTTP.responseXML
setobjXSL=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
endif
SetobjHTTP=Nothing
SetobjXML=Nothing
SetobjXSL=Nothing
%>
style.xsl:
<xsl:stylesheetxmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:templatematch="/">
<html>
<head>
<TITLE>moreover...</TITLE>
</head>
<bodyBGCOLOR="ffffff">
<DIVALIGN="center">
<TABLEBGCOLOR="ffffff"BORDER="0"CELLPADDING="4"CELLSPACING="0"WIDTH="100%">
<xsl:for-eachselect="moreovernews/article">
<TRVALIGN="middle">
<TDALIGN="left"BGCOLOR="ffffff">
<xsl:attributename="HREF">
<xsl:value-ofselect="url"/>
</xsl:attribute>
<xsl:attributename="TARGET">
_blank
</xsl:attribute>
<xsl:value-ofselect="headline_text"/>
<xsl:attributename="HREF">
<xsl:value-ofselect="document_url"/>
</xsl:attribute>
<xsl:attributename="TARGET">
_blank
</xsl:attribute>
<xsl:value-ofselect="source"/>
<xsl:attributename="HREF">
<xsl:value-ofselect="access_registration"/>
</xsl:attribute>
<xsl:attributename="TARGET">
_blank
</xsl:attribute>
<xsl:value-ofselect="access_status"/>
<xsl:value-ofselect="harvest_time"/>GMT
</TD>
</TR>
</xsl:for-each>
</TABLE>
</DIV>
</body>
</html>
</xsl:template>
</xsl:stylesheet>