Write an asynchronous multi-thread download program with VB

xiaoxiao2021-03-06  40

In order to highly efficiently carry a web page

,

We can use

VB

of

Internet Transfer. INTERNET TRANSFER

Control writes your own download

, Internet Transfer, INTERNET TRANSFER

Control support hypertext transfer protocol

(Http)

And file transfer protocol

(Ftp)

,use

Internet Transfer. INTERNET TRANSFER

The control can pass

OpenURL

or

EXECUTE

The method is connected to any site that uses the two protocols and retrieves files. This program uses multiple

Internet Transfer. INTERNET TRANSFER

The control makes it simultaneously downloads a certain site. And can determine if the file has been downloaded or downloaded by the current file is old on the server, to decide whether to re-download. The links in all downloaded files have been adjusted to see them locally.

OpenURL

The method is transmitted in synchronously. The synchronization refers to the other process cannot be performed until the transmission operation is not completed. Such data transmission must be done before performing other codes.

and

EXECUTE

The method is transmitted asynchronously. In call

EXECUTE

When the method, the transmission operation is independent of other processes. In this way, call

EXECUTE

After the method, other code can be performed while receiving data in the background.

use

OpenURL

The method can directly get the data stream that can be saved to the disk, or directly

Textbox

Reading in the control (if the data is text format). Use

EXECUTE

Methods to obtain data, you must use

StateChanged

Event monitor the connection status of the control. When reaching the appropriate state, call

Getchunk

The method obtains data from the buffer of the control.

First of all, establish start-up

HTTP

Retrieve connection

, Public G As Variant Public K As Variant Public Spath As String Dim Links () AS String G = 0 spath =

Locally saved the path to download files

Links (0) =

Start

URL INET1.EXECUTE LINKS (0), "Get"

Note: Use

Get

method.

Event monitoring subroutine (each

Internet Transfer. INTERNET TRANSFER

Control settings corresponding to event monitoring subroutines):

use

StateChanged

Event monitoring the connection status of the control

,

When the request has been completed, all data has been received, call

Getchunk

The method obtains data from the buffer of the control.

Private sub inet1_statechanged (Byval State As Integer)

Note:

State = 12

Time, use

Getchunk

Method retrieves the response of the server.

Select Case State

Note:

...

There is no other case.

Case icrSponseCompleted

Note:

12

Note: Get

Links (g)

Agreement, host and path name.

Addsuf = Left (Links (g), Instrrev (Links (g), "/"))

Note: Get

Links (g)

The file name in the middle.

FNAME = Right (LINKS (G), Len (Links (g)) - Instrrev (Links (G), "/"))

Note: It is determined whether it is a hypertext file, which is a hypertext file, and the link is analyzed. If it is not a binary file.

IF INSTR (1, FNAME, "HTM", VbtextCompare) = true kil

Note: Initialization is used to save the file

FileSystemObject

Object.

SET FS = CreateObject ("scripting.filesystemObject") DIM VTDATA AS VARIANT Note: Data variables.

DIM STRDATA AS STRING: STRDATA = "" DIM BDONE AS BOOLEAN: BDONE = FALSE

Note: Take the first piece.

VTData = inet1.getchunk (1024, ICString) Doevents Do While Not Bdone Strdata = STRDATA & VTDATA DOEvents

Note: Get the next piece.

VTData = inet1.getchunk (1024, ICString) if len (vTData) = 0 Then bdone = true end if loop

Note: Get the link in the document and placed in an array.

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)

Note: Change the original link to a new link

Oril = MID (STRDATA, PO1 1, PO2 - PO1 - 1)

Note: If there is quotation mark, remove the quotation mark

Ln = Replace (Oril, "" "" "" "" "" "" "" "" "" "" "" "" "

Note: Determines if the file is downloaded.

If FileExists (Spath & newl) = false the links (i) = addsuf & ln i = i 1 else lmtime = inet1.getHeader ("Last-modified") set f = fs.getfile (spath & newl) ctime = f .datecreated

Note: Judging whether the file is updated

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 = inet2.getchunk (1024, IcByteArray) do while not bdone b () = b () & vtata vtdata = inet2.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 Note: Calling thread schedule

End SELECT

End Sub

Private sub inet2_statechanged (Byval State As Integer) ... End Sub

...

Thread scheduler

, G

Sum

k

Public variable,

k

Adding one for the array of the last link,

G

At the beginning of zero, each time you add one until the last link is processed.

Private sub devjob ()

IF not g 1

...

ReportLine: if inet1.stillexecuting = false and inet2.stillexecuting = false and ... the msgbox ("

Download the end.

") End if End Sub

转载请注明原文地址:https://www.9cbs.com/read-65913.html

New Post(0)