Write an asynchronous multi-thread download program with VB

zhaozj2021-02-11  203

Author: Daqing Oilfield Exploration and Development Research Institute Co., internet room full of filial piety in order to efficiently download page of a site, we can use the Internet Transfer Control VB to write your own program download, Internet Transfer control supports Hypertext Transfer Protocol (HTTP) and File Transfer Protocol (FTP), using the Internet Transfer control can be connected to any site that uses the two protocols and retrieves files via the OpenURL or Execute method. This program uses multiple Internet Transfer controls to download a site at the same time. 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. The OpenURL method transmits data 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. The Execute method transmits data asynchronously. When the Execute method is called, the transfer operation is independent of other processes. Thus, after the Execute method is called, other code can be performed while receiving data in the background. With the OpenURL method, you can directly get the data stream that can be saved to the disk, or reading directly in the TextBox control (if the data is text format). With the Execute method to obtain data, you must monitor the connection status of the control with the STATECHANGED event. When the appropriate state is reached, the GetChunk method is called to obtain data from the buffer of the control. First, establish the start-up HTTP retrieval connection, PUBLIC G AS VARIANT PUBLIC K AS VARIANT PUBLIC SPATH AS STRING DIM LINKS () AS STRING G = 0 Spath = Locally Save Downloads Links LINKS (0) = Subsitates URL INET1.EXECUTE Links (0), "get" 'uses the GET method. Event Monitoring Subprogram (Event Monitor Sprinter for Each Internet Transfer Control Settings): Monitor the connection status of the control with the StateChanged event, and all the data has been completed, and all the data has been received, call the getchunk method from the control The buffer acquires data. Private Sub inet1_statechanged (Byval State As Integer) When you use the getchunk method to retrieve the server's response. SELECT CASE State '... did not list other situations. Case icrSponseCompleted '12 'Gets protocols, hosts, and path names in Links (g). Addsuf = Left (Links (g), INSTRREV (Links (g), "/")) 'Gets file names in Links (g). FNAME = Right (Links (g)) - INSTRRRRREV (Links (g), "/")) 'Judging whether it is a hypertext file, it is a hypertext file to analyze the link, if not Save binary files. If INSTR (1, FNAME, "HTM", vbtextCompare) = true kiln initializes the FileSystemObject object for saving files. SET FS = CreateObject ("scripting.filesystemObject") DIM VTDATA AS VARIANT 'data variable.

DIM STRDATA AS STRING: STRDATA = "" DIM BDONE AS BOOLEAN: BDONE = false 'acquisition first. VTData = inet1.getchunk (1024, ICString) Doevents do while not bdone strdata = strdata & vtdata doevents' gets the next piece. VTDATA = INET1.GETCHUNK (1024, ICSTRING) If LEN (VTDATA) = 0 THEN BDONE = true end if loop '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) ' Change the original link to new link oril = MID (strdata, po1 1, po2 - po1 - 1) 'If there is quotation mark, remove quotation marks ln = replace (oril, "" ",", ", vbtexTCompare) newl = Right ( Ln, Ln (LN) - Instrrev (ln, "/")) whostr = whostr & newl if ln <> "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "The 'decision file is downloaded.

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

New Post(0)