日期:2012-01-04  浏览次数:21213 次

HTTP组件,我所知道的有ASPHTTP,ASPTEAR等等,这里我们要用的是比尔的XMLHTTP,如果你机器上没有,可以到这里下载

http://www.microsoft.com/data 下载mdac2.6

http://msdn.microsoft.com/workshop/xml/index.asp 下载msxml

象大多数我们看到的页面一样,比如一个新闻具体内容页面,该页面有个连接叫打印本页,点进去以后,并不是立即打印(当然也可以做到,我指的是选择性打印),而是出现一个去掉一些头尾等无关信息的文件,这里也许有个打印连接,也许要你自己动手了。

我们的思路是这样的,用HTTP组件获得该页面的信息,然后用一些方法比如我们可以采用正则表达式来分离我们想要的和不想要的东西。我们用HTML的标签来剔除我们不想要的内容,比如:

<!-- START PPOMIT -->
<table>
  <tr>
    <td><a href="/home">home page</a></td>
  </tr>
  <tr>
    <td><a href="/support">support</a></td>
  </tr>
  <tr>
    <td><a href="/contact">contact us</a></td>
  </tr>
  <tr>
    <td><a href="/products">Products</a></td>
  </tr>
</table>
<!-- END PPOMIT -->

当然,还需要提供一个打印连接,我们用JSCRIPT来做。
<script Language="JavaScript">
<!-- hide from old browsers
  // ppb and ppe are the start and end of the dynamic link
  // window.location.href will refer to the current URL of the page
  // it's nice to make it open in a new window too!  i.e. target=_blank
     
    var ppb = "<a href=/print/printpage.asp?ref=";
     var ppe = " target=_blank>Print This Page</a><br>";
     document.write(ppb + window.location.href + ppe);

//end -->
</script>

这里我们传递了一个当前路径给打印页面,请看呆会的代码

先举个xmlhttp的简单例子
<%

Function GetHTML(strURL)
  Dim objXMLHTTP, strReturn
  Set objXMLHTTP = SErver.CreateObject("Microsoft.XMLHTTP")

  objXMLHTTP.Open "GET", strURL, False
  objXMLHTTP.Send

  strReturn = objXMLHTTP.responseText
  Set objXMLHTTP = Nothing

  GetHTML = strReturn
End Function

' Write it:
Response.Write GetHTML("http://www.topcoolsite.com/")

' Download it:
Response.ContentType = "application/x-msdownload"
Response.AddHeader "Content-Disposition", "filename=Something.asp"
Response.BinaryWrite GetHTML("http://www.topcoolsite.com/")

%>
运行上面这段代码就能看到我的个人网站了,:)

接下来我们步入正题,我们创建一个printpage.asp
一旦你在合适的位置插入了注释标记,剩下来的只是代码的事了

<%
option explicit
Response.Buffer = True

'定义变量,正则表达式,XMLHTTP,路径

Dim RefPage, objXMLHTTP, RegEx
   
RefPage = Request.QueryString("ref")
if RefPage = "" then
   response.write "<h3>非法路径</h3>"
   response.end
end if

Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")

  
objXMLHTTP.Open "GET", RefPage, False
objXMLHTTP.Send

RefPage = objXMLHTTP.responseText

Set RegEx = New RegExp
RegEx.Global = True


'利用正则表达式来干活
RegEx.Pattern =  "<!-- START PPOMIT -->"

RefPage = RegEx.Replace(refpage,( chr(253) ))

RegEx.Pattern = "<!-- END PPOMIT -->"

RefPage = RegEx.Replace(refpage,( chr(254) ))

RegEx.Pattern = chr(253) & "[^" & chr(254) & "]*" & chr(254)

RefPage = RegEx.Replace(refpage, " " )

Set RegEx = Nothing
Set objXMLHTTP = Nothing

Response.W