为了高效率地下载某站点的网页,我们可利用vb的inte.net transfer 控件编写自己的下载程序, inte.net transfer 控件支持超文本传输协议 (http) 和文件传输协议 (ftp),使用 inte.net transfer 控件可以通过 openurl 或 execute 方法连接到任何使用这两个协议的站点并检索文件。 本程序使用多个inte.net transfer 控件,使其同时下载某站点。并可判断文件是否已下载过或下载过的文件是否比服务器上当前的文件陈旧,以决定是否重新下载。所有下载的文件中的链接都做了调整,以便于本地查阅。
openurl 方法以同步方式传输数据。同步指的是传输操作未完成之前,不能执行其它过程。这样数据传输就必须在执行其它代码之前完成。
而 execute 方法以异步方式传输数据。在调用 execute 方法时,传输操作与其它过程无关。这样,在调用 execute 方法后,在后台接收数据的同时可执行其它代码。
用 openurl 方法能够直接得到可保存到磁盘的数据流,或者直接在 textbox 控件中阅览(如果数据是文本格式的)。而用 execute 方法获取数据,则必须用 statechanged 事件监视该控件的连接状态。当达到适当的状态时,调用 getchunk 方法从控件的缓冲区获取数据。
首先,建立启始的http检索连接,
| public g as variant public k as variant public spath as string dim links() as string g = 0 spath = 本地保存下载文件的路径 links(0)=启始url .net1.execute links(0), "get" 注释:使用get方法。 |
| private sub .net1_statechanged(byval state as integer) 注释:state = 12 时,使用 getchunk 方法检索服务器的响应。 select case state 注释:...没有列举其它情况。 case icresponsecompleted 注释:12 注释:获取links(g)中的协议、主机和路径名。 addsuf = left(links(g), instrrev(links(g), "/")) 注释:获取links(g)中的文件名。 fname = right(links(g), len(links(g)) - instrrev(links(g), "/")) 注释:判断是否是超文本文件,是超文本文件则分析其中的链接,若不是则存为二进制文件。 if instr(1, fname, "htm", vbtextcompare) = true then 注释:初始化用于保存文件的filesystemobject对象。 set fs = createobject("scripting.filesystemobject") dim vtdata as variant 注释:数据变量。 dim strdata as string: strdata = "" dim bdone as boolean: bdone = false 注释:取得第一块。 vtdata = .net1.getchunk(1024, icstring) doevents do while not bdone strdata = strdata & vtdata doevents 注释:取得下一块。 vtdata = .net1.getchunk(1024, icstring) if len(vtdata) = 0 then bdone = true end if loop 注释:获取文档中的链接并置于数组中。 dim i as variant dim po1 as variant dim po2 as variant dim oril as string dim newl as string dim lmtime, ctime po1 = instr(1, strdata, "href=", vbtextcompare) + 5 po2 = 1 dim newstr as string: newstr = "" dim whostr as string: whostr = "" i = 0 do while po1 > 0 newstr = mid(strdata, po2, po1) whostr = whostr + newstr po2 = instr(po1, strdata, ">", vbtextcompare) 注释:将原链接改为新链接 oril = mid(strdata, po1 + 1, po2 - po1 - 1) 注释:如果有引号,去掉引号 ln = replace(oril, """", "", vbtextcompare) newl = right(ln, len(ln) - instrrev(ln, "/")) whostr = whostr & newl if ln <> "" then 注释:判定文件是否下载过。 if fileexists(spath & newl) = false then links(i) = addsuf & ln i = i + 1 else lmtime = .net1.getheader("last-modified") set f = fs.getfile(spath & newl) ctime = f.datecreated 注释:判断文件是否更新 if datediff("s", lmtime, ctime) < 0 then i = i + 1 end if end if end if po1 = instr(po2 + 1, strdata, "href=", vbtextcompare) + 5 loop newstr = mid(strdata, po2) whostr = whostr + newstr set a = fs.createtextfile(spath & fname, true) a.write whostr a.close k = i else dim vtdata as variant dim b() as byte dim bdone as boolean: bdone = false vtdata = .net2.getchunk(1024, icbytearray) do while not bdone b() = b() & vtdata vtdata = .net2.getchunk(1024, icbytearray) if len(vtdata) = 0 then bdone = true end if loop open spath & fname for binary access write as #1 put #1, , b() close #1 end if call devjob 注释:调用线程调度子程序 end select end sub private sub .net2_statechanged(byval state as integer) ... end sub ... |
| private sub devjob() if not g + 1 < k then goto reportline if .net1.stillexecuting = false then g = g + 1 .net1.execute links(g), "get" end if if not g + 1 < k then goto reportline if .net2.stillexecuting = false then g = g + 1 .net2.execute links(g), "get" end if ... reportline: if .net1.stillexecuting = false and .net2.stillexecuting = false and ... then msgbox ("下载结束。") end if end sub |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器