Use the XMLHTTP component to implement multipart breakpoint resume under the HTTP protocol

Source: Internet
Author: User

Use XMLHTTP to download files transmitted over HTTP. If the file is large, you can generally consider using an Asynchronous Method. However, XMLHTTP Asynchronization in VB still causes thread blocking. How to avoid this situation, this article provides a demonstration:

'*************************************** * <Br/> '* Module name: form1.frm <br/> '* module function: Use the XMLHTTP component to implement resumable data transfer over HTTP <br/>' * Author: lyserver <br/> '********************************** * ***** </P> <p> dim m_blnstop as Boolean </P> <p> '* --------------------------------------------- <br/>' function description: XMLHTTP-based data download function (supports resumable data transfer) <br/> 'parameter description: URL: the URL to be downloaded. <br/> 'filename' indicates the file that stores the download result. <br/> 'blocksize: The size varies depending on the network conditions, so that VB can execute doevents in time to obtain mouse and keyboard actions And refresh the page <br/> 'resumable transfer supported by resumetransfer <br/> 'Encoding: lyserver <br/> '* response <br/> private sub downfilebyhttp (byval URL as string, filename as string, optional blocksize as long = 4096, optional resumetransfer as Boolean) <br/> dim XMLHTTP as object <br/> dim bytdata () as byte <br/> dim I as long, FN as integer, ltotalsize as long </P> <p> 'get the file length <br/> set XMLHTTP = CRER Ateobject ("msxml2.xmlhttp") <br/> XMLHTTP. open "head", URL, false <br/> XMLHTTP. send <br/> ltotalsize = XMLHTTP. getResponseHeader ("Content-Length") <br/> set XMLHTTP = nothing </P> <p> 'open a file <br/> fn = freefile () <br/> open filename for binary as # FN </P> <p> 'indicates whether resumable data transfer is required. <br/> If resumetransfer = true then <br/> I = lof (FN) <br/> seek FN, I + 1 <br/> end if </P> <p> 'multipart download data and save it to the file <br/> m_blnstop = Fal Se 'reset the interrupt flag <br/> do while I <ltotalsize and m_blnstop = false' The for loop is not used because the number of downloads required may be different from the actual number of downloads. <br/> 'file data <br/> set XMLHTTP = Createobject ("msxml2.xmlhttp ") <br/> XMLHTTP. open "get", URL, false <br/> XMLHTTP. setRequestHeader "Referer", left (URL, instr (URL, "//") + 2, URL, "/")-1) <br/> XMLHTTP. setRequestHeader "accept", "*/*" <br/> XMLHTTP. setRequestHeader "User-Agent", "lyserver" <br/> X Mlhttp. setRequestHeader "range", "bytes =" & I & "-" & CSTR (I + IIF (ltotalsize-I> blocksize, blocksize, ltotalsize-I)-1) 'segment <br/> XMLHTTP. setRequestHeader "Content-Type", "application/octet-stream" <br/> XMLHTTP. setRequestHeader "Pragma", "No-Cache" <br/> XMLHTTP. setRequestHeader "cache-control", "No-Cache" <br/> XMLHTTP. send <br/> 'To a byte array <br/> bytdata = XMLHTTP. responsebody <br/> set XML HTTP = nothing <br/> 'Save to file <br/> put FN ,, bytdata <br/> 'reset the download start location <br/> I = I + ubound (bytdata) + 1 <br/> 'release Control <br/> doevents <br/> 'shows the progress <br/> debug. print VBA. formatpercent (I/ltotalsize) <br/> loop <br/> close FN <br/> end sub </P> <p> private sub commandementclick () <br/> 'download. Due to the test in the LAN, the block setting is large (400 k). <br/> downfilebyhttp "http: // 192.168.1.134/vbe600chs1.rar", "C: /temp.rar ", 409600, true <br/> If M_blnstop = true then <br/> msgbox "data download interrupted! ", Vbcritical," prompt "<br/> else <br/> msgbox" data download is complete! ", Vbinformation," prompt "<br/> end if <br/> end sub </P> <p> private sub command2_click () <br/> 'interrupted download <br/> m_blnstop = true <br/> end sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.