Programming and Application(编程与应用)


Content(目录)




Linux


MySQL
Office















 
PCNow 30-Day Free Trial, Remote PC Access
 
Logo_234x60

ASP网站远程抓取文档的方法


ASP网站远程抓取文档的方法

河南济源 董占山

1.引子

在我国,由于我们的网络设施还比较落后,网络的速度往往不如人意,所以出现了网络蚂蚁,来帮助加速网络下载,给国人乃至世界上很多使用网络的人提供了极大的方便。网络蚂蚁不仅加快了下载速度,同时减少了Modem的空转时间,节省了时间和金钱。但是如果你是一个网络管理员,在远程控制服务器时,若想在网络上下载一个文档,并存入服务器的相关位置供用户使用时,你会怎样做呢? 下载到本地,然后再复制或上载到服务器?具我所知,不少人仍然这样做,这不仅浪费时间和金钱,还消耗掉大量的精力。

我在构建一个ASP网站的过程中,编写了大量的程序,实现了网站创建和更新的自动化,其中,为了实现网站之间直接交换文档,在网上查阅了大量的文章,找到了一个简捷的方法,用DELPHI编写了一个小巧的COM组件,从而实现了让服务器直接跨站抓取远程文档的自动化。后来,在使用网络蚂蚁的过程中,由于其优秀的下载性能,萌发了将其作为下载服务器的想法,这样可以大大加快下载速度。下面就分别介绍这两种实现方法。

2.用DELPHI创建一个COM组件

本组件使用Windows 98 + Delphi 5.0创建,经验证在Windows 98 + Delphi 6.0下同样可以工作。

2.1 创建一个新的ActiveX库工程

单击“ File”菜单中的“New”菜单项,打开“ New Items”对话窗口,单击“ActiveX”标签切换到ActiveX对话页(图 1),双击“ActiveX Library”图标创建一个新的DLL类库工程。

图 1 ActiveX对话页

2.2 创建一个自动类(automation class)

打开“New Items”对话窗口,切换到“ActiveX”对话页 ,双击“Automation Object”图标,打开“自动对象向导(Automation Object Wizard)”,如图 2所示,在“CoClass Name”编辑框中输入“ReadUrl”,单击“OK”按钮,Delphi将建立自动类的框架,接着弹出一个“类库(Type Library)”对话窗口。单击“File”菜单中的“Save”菜单项,将自动类单元保存为uMain.pas,将类库工程保存为“ReadUrl.dpr”。

图 2 自动对象向导

2.3 添加方法

本组件只包含1个方法。为自动类添加一个方法较简单,单击“Edit”菜单中的“Add to Interface”菜单项,弹出“Add To Interface”对话窗口(图 3)。在“Declaration”编辑框中输入成员函数或过程的声明,“function DownloadFile(Soure, Dest: OleVariant) : OleVariant;”,单击“OK”按钮创建方法。Delphi自动引导你到自动类单元的方法实现部分,等待你输入代码完成该方法。

图 3 添加COM组件方法对话窗口

2.4 源程序代码

本文提供的ReadUrl组件包含3个源程序文件:工程源文件ReadUrl.DPR、类库源文件ReadUrl_TLB.pas和自动类单元文件uMain.Pas,其中,你只需要也只能更改自动类单元文件的内容,其余两个文件由Delphi来维护,所以下面只列出了自动类源文件的内容。

ReadUrl.PAS

unit uMain;

interface

uses
ComObj, ActiveX, ReadUrl_TLB, StdVcl;

type
TReadUrl = class(TAutoObject, IReadUrl)
protected
function DownloadFile(source, dest: OleVariant): OleVariant; safecall;
{ Protected declarations }
end;

implementation

uses ComServ, UrlMon;

function TReadUrl.DownloadFile(Source, Dest: OleVariant): OleVariant;
var
ASource, ADest : string;
begin
ASource := Source;
ADest := Dest;
Try
Result := UrlDownloadToFile(nil,PChar(ASource),PChar(ADest),0,nil) = 0;
except
Result := False;
end;
end;

initialization
TAutoObjectFactory.Create(ComServer, TReadUrl, Class_ReadUrl,
ciMultiInstance, tmApartment);
end.

2.5 COM组件的使用

若你正确地完成了上述步骤,现在就可编译生成COM组件了。编译完成后,单击“Run”菜单中的“Register ActiveX Server”菜单项,将新生成的COM组件在Windows系统下注册。注册成功后,你就可以在ASP页面中使用该COM组件了。在ASP页面中使用脚本语言VB Script的Server.CreateObject来创建一个ReadUrl对象实例的命令如下:

Set objTool = Server.CreateObject("ReadUrl.ReadUrl")

下面提供了一个使用该组件的ASP页面实例(fetch.asp)。首先需要把这个ASP程序放到ASP服务器的相应目录下,用浏览器打开此页面,在表单中输入特定的URL地址和一个说明型的标题,输入完成后,单击“Submit”按钮,表单中的信息就被送到服务器进行处理,该ASP页面通过ReadUrl下载指定地址的文档,将上传文件保存到你服务器的根目录下,当然你可以改变语句“whichfile=server.mappath("\" & strDest)”中的“\”,来确定目标目录。该ASP例程在Windows 98 + PWS网络环境下调试通过。

Fetch.ASP

<%
option explicit
response.buffer=true
Server.ScriptTimeout = 10000

%>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Download any file from a URL address</title>
</head>

<body>

<h1>Download a file from a URL address</h1>
<%

Dim objTool 'Dimension object variable
dim DownloadStatus, strSource, strDest, strTitle, whichfile

Set objTool = Server.CreateObject("readUrl.readUrl")
DownloadStatus = "No file downloaded"
strSource = request("downloadUrl")

if (strSource <> "") then
strDest = GenFileName
strTitle = request("title")
strDest = strDest + getDefaultFileName(strSource)
whichfile=server.mappath("\" & strDest)
DownloadStatus = strDest & " is downloaded "
if objTool.DownloadFile(strSource,whichfile) then
DownloadStatus = DownloadStatus + "successfully"
else
DownloadStatus = DownloadStatus + "UNSUCCESSFULLY"
end if
end if
set objTool = nothing
%>

<form method="POST" action="<% =Request.ServerVariables("SCRIPT_NAME") %>">
<table class="top" cellspacing="4">
<tr>
<td class="left">Status</td>
<td class="right"><font color="red"><% =DownloadStatus %> </font></td>
</tr>
<tr>
<td class="left">URL</td>
<td class="right"><input type="text" name="downloadUrl" size="57"></td>
</tr>
<tr>
<td class="left">Title</td>
<td class="right"><input type="text" name="title" size="57"></td>
</tr>
<tr>
<td class="left"></td>
<td class="right"><input type="submit" value="Submit" name="B1"><input
type="reset" value="Reset" name="B2"></td>
</tr>
</table>
</form>
</body>
</html>
<%
Server.ScriptTimeout = 90

%>
<script Runat="Server" Language="VBScript">

function GetDefaultFileName(Source)
dim strTemp1, strTemp2, pos, head, breakchar, pos1
strTemp1 = ""
strTemp2 = Source
pos = 1
head = "file://"
breakchar = "/"
if Instr(pos,strTemp2,head) > 0 then
if Instr(pos,strTemp2,"\") > 0 then breakchar = "\"
end if
pos1 = Instr(pos,strTemp2,breakchar)
strTemp1 = Mid(StrTemp2,pos1+1,len(strTemp2)-pos1)
while (pos1 > 0)
pos = pos1
strTemp2 = strTemp1
pos1 = Instr(1,strTemp2,breakchar)
strTemp1 = Mid(StrTemp2,pos1+1,len(strTemp2)-pos1)
wend
pos = Instr(1,strTemp1,",")
if pos > 0 then strTemp1 = mid(strTemp1,1, pos-1)
pos = Instr(1,strTemp1,"\")
if pos > 0 then strTemp1 = mid(strTemp1,1, pos-1)
pos = Instr(1,strTemp1,"/")
if pos > 0 then strTemp1 = mid(strTemp1,1, pos-1)
pos = Instr(1,strTemp1,"?")
if pos > 0 then strTemp1 = mid(strTemp1,1, pos-1)
pos = Instr(1,strTemp1,":")
if pos > 0 then strTemp1 = mid(strTemp1,1, pos-1)
pos = Instr(1,strTemp1,"*")
if pos > 0 then strTemp1 = mid(strTemp1,1, pos-1)
pos = Instr(1,strTemp1,"""")
if pos > 0 then strTemp1 = mid(strTemp1,1, pos-1)
pos = Instr(1,strTemp1,"<")
if pos > 0 then strTemp1 = mid(strTemp1,1, pos-1)
pos = Instr(1,strTemp1,">")
if pos > 0 then strTemp1 = mid(strTemp1,1, pos-1)
pos = Instr(1,strTemp1,"|")
if pos > 0 then strTemp1 = mid(strTemp1,1, pos-1)
GetDefaultFileName = strTemp1
end function

function GenFilename
dim strTemp
strTemp = ""
strTemp = DatePart("yyyy",date) &_
DatePart("m",date) &_
DatePart("d",date) & "-" &_
hour(time) &_
minute(time) &_
second(time)
GenFileName = strTemp
end function
</script>

3. 利用网络蚂蚁NETANTS

网络蚂蚁不仅速度快, 管理下载文档方便, 还提供了一个简单的编程界面(NetAnts.API), 我们可以通过该API界面在ASP中用服务器端运行的VB Script来控制网络蚂蚁,实现下载数据的目的。其实网络蚂蚁本身的一些功能也是通过客户端VB Script来实现的。

3.1 网络蚂蚁的设置

在使用网络蚂蚁之前,需要对它进行必要的设置。首先是设定文档的保存路径,这里设定的是“c:\inetpub\wwwroot\test”,见图 4所示,注意在你的服务器上,设定的目录必须存在。

图 4 网络蚂蚁设置默认下载路径的对话页

其次是对高级选项页(图 5)上的默认设置进行修改,选中下列选项:

  • Use cookie from browser
  • Auto save job every
  • Delete job from list after complete
  • Exit after done downloading
  • Backup download database every day

选空本页上所有的其他选项。其他设置一概使用蚂蚁的默认设置。本文使用的网络蚂蚁是NetAnts 1.24版。

图 5 网络蚂蚁高级选项的设定

3.2 调用网络蚂蚁的ASP程序

在ASP页面中使用脚本语言VB Script的CreateObject来创建一个网络蚂蚁API的对象实例:

set NetAntsApi=CreateObject("NetAnts.API")

下面提供了一个使用网络蚂蚁API的ASP页面实例(nafetch.asp)。该ASP页面的使用方法如上所述。该ASP例程在Windows 98 + PWS网络环境下调试通过。

Nafetch.asp

<%
option explicit
response.buffer=true
Server.ScriptTimeout = 10000
%>
<h1>Download a file from a URL address</h1>
<%

dim DownloadStatus, strSource, strTitle, whichfile
DownloadStatus = "No file downloaded"
strSource = request("downloadUrl")
if (strSource <> "") then
strTitle = request("title")
call AddLink(strSource,strTitle)
DownloadStatus = strSource & " is downloaded "
end if
%>
<form method="POST" action="<% =Request.ServerVariables("SCRIPT_NAME") %>">
<table class="top" cellspacing="4">
<tr>
<td class="left">Status</td>
<td class="right"><font color="red"><% =DownloadStatus %> </font></td>
</tr>
<tr>
<td class="left">URL</td>
<td class="right"><input type="text" name="downloadUrl" size="57"></td>
</tr>
<tr>
<td class="left">Title</td>
<td class="right"><input type="text" name="title" size="57"></td>
</tr>
<tr>
<td class="left"></td>
<td class="right"><input type="submit" value="Submit" name="B1"><input
type="reset" value="Reset" name="B2"></td>
</tr>
</table>
</form>
<%
Server.ScriptTimeout = 90
%>
<script Runat="Server" Language="VBScript">
Sub AddLink(Url,Info)
dim NetAntsApi
'On Error Resume Next
set NetAntsApi=CreateObject("NetAnts.API")
if err<>0 then
response.write "NetAnts not properly installed on this PC!<br>"
else
if NetAntsApi.IsUrlExist(Url) then
response.write Url+vbCrLf+"already in queue<br>"
else
call NetAntsApi.AddUrl(Url, Info,"")
end if
end if
end sub
</script>

4. 小结

上述两种技术各有其优缺点,COM组件下载数据较慢,但支持非西文文件名和路径,用户可以指定下载的目标路径;网络蚂蚁下载速度较快,但不支持中文路径与文件名称,同时用户不能指定下载的目标路径,因其API函数中没有给出相应的参数,只能下载到一个事先设定好的目标路径。

笔者在创建个人网站( http://www-personal.ksu.edu/~zhanshan)时结合使用了本文所述的技术,在笔者的网页上有一个Fetch的命令,使用的是第一种技术,及ReadUrl组件。该网站上的很多文档是用该组件抓来的。我的电子邮件地址是dong_zhanshan@hotmail.com,欢迎与我联系。

©董占山Zhanshan Dong

Post comments(留言)

Name(名字):

Comment(内容):


由Google提供

SunfineData Products|U's Bargain Network|Contact Me(与我联系)
© 1998-, 董占山, 版权所有, 欢迎转载文章链接。
转载文章和软件请注明出处(http://articles.sunfinedata.com/)。