利用XMLHTTP 从其他页面获取数据

我们在编写ASP代码的时候,大家都知道可以通过post或者get获得form表单的数据,那么我们如何直接获得其他页面上的数据呢?这就要借助xmlhttp协议了。xmlhttp是xmldom技术的一部分。

下面的代码就是一个很简单的例子,我们利用xmlhttp技术,把 http://www.codetoad.com/ 站点首页的代码以xml的形式完全获取,并且在页面中输出。

 1   
 2Dim objXMLHTTP, xml   
 3Set xml = Server.CreateObject("Microsoft.XMLHTTP")   
 4  
 5xml.Open "GET", " http://www.codetoad.com/ ", False   
 6' Pull the data from the web page   
 7xml.Send 
 8
 9Response.write "Here's the html we now have in our xml object"   
10Response.write "

<br/>

<br/>

<br/>

1"   
2Response.Write "

<xmp>"
Response.Write xml.responseText
Response.Write "</xmp>

1"   
2Response.write "

<br/>

<br/>

<br/>

1"   
2Response.write " Now here's how the page looks:

<br/>

<br/>

1"   
2Response.Write xml.responseText   
3  
4Set xml = Nothing   

下面是另一个实例

 1   
 2dim objHTTP , objXML , objXSL   
 3set objHTTP = Server.CreateObject("Microsoft.XMLHTTP")   
 4objHTTP.open "GET", " http://p.moreover.com/cgi-local/page?c=Pop%20music%20reviews&o=xml ", false   
 5objHTTP.send   
 6set objXML = objHTTP.responseXML   
 7set objXSL=Server.CreateObject("microsoft.xmldom")   
 8objXSL.async=false 
 9
10objXSL.load(Server.MapPath("style.xsl")) 
11
12if (objXSL.parseError.errorCode = 0) then   
13Response.Write(objXML.transformnode(objXSL))   
14else   
15Response.Write "Error: " & objXSL.parseError.reason & " URL:" & objXSL.url   
16end if 
17
18Set objHTTP = Nothing   
19Set objXML = Nothing   
20Set objXSL = Nothing   

style.xsl:

 1<xsl:stylesheet xmlns:xsl=" http://www.w3.org/TR/WD-xsl ">
 2<xsl:template match="/">
 3<html>
 4<head>
 5<title>moreover...</title>
 6</head>
 7<body bgcolor="ffffff">
 8<div align="center">
 9<table bgcolor="ffffff" border="0" cellpadding="4" cellspacing="0" width="100%">
10<xsl:for-each select="moreovernews/article">
11<tr valign="middle">
12<td align="left" bgcolor="ffffff">
13<xsl:attribute name="HREF">
14<xsl:value-of select="url"></xsl:value-of>
15</xsl:attribute>
16<xsl:attribute name="TARGET">   
17_blank   
18</xsl:attribute>
19<xsl:value-of select="headline_text"></xsl:value-of>
20<xsl:attribute name="HREF">
21<xsl:value-of select="document_url"></xsl:value-of>
22</xsl:attribute>
23<xsl:attribute name="TARGET">   
24_blank   
25</xsl:attribute>
26<xsl:value-of select="source"></xsl:value-of>
27<xsl:attribute name="HREF">
28<xsl:value-of select="access_registration"></xsl:value-of>
29</xsl:attribute>
30<xsl:attribute name="TARGET">   
31_blank   
32</xsl:attribute>
33<xsl:value-of select="access_status"></xsl:value-of>
34<xsl:value-of select="harvest_time"></xsl:value-of> GMT   
35  
36</td>
37</tr>
38</xsl:for-each>
39</table>
40</div>
41</body>
42</html>
43</xsl:template>
44</xsl:stylesheet>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus