Use the Winsock download file (support breakpoints)

xiaoxiao2021-03-06  58

The first step, established an engineering, reference Winsock (Visual Basic is best playing SP6, otherwise MS has a bug), omitted here

In the second step, the specific implementation code Step 1: Send a request

Description:

(1) Simple adoption of the judgment already has the same name file to indicate whether there is a breakpoint.

(2) Download address, size, and downloaded bytes are simply present in the INI file, safer practice this article does not discuss

Interested friends can contact me

'------------------------------------- -------------------------------

'Name: Downloadfile

'Author: REKER 2004/3/20

'DESC: Connect the remote host, send a receiving file request, wait for a remote host response

'Params: None

'History: None

'------------------------------------- -------------------------------

Private sub downloadFile ()

ON Error ResMe next

StartTime = Time ()

WITH WINSCK

.Remotehost = host 'distal host address

.Remoteport = 80

.Connect

'Waiting for the server connection

Do while .state <> SCKCONNECTED

Doevents: doevents: doevents: doevents

'20 second timeout

IF Datediff ("S", StartTime, Time ())> 20 THEN

ShowInfo "Connection timeout"

.Close

EXIT SUB

END IF

Loop

'Send download file request

'Use HTTP / 1.0 protocol here

STRCOMMAND = "get" UpdateURL "http / 1.0" vbcrlf "download address

STRCOMMAND = STRCOMMAND "accept: * / *" vbcrlf "Don't

STRCOMMAND = STRCOMMAND "Accept: text / html" VBCRLF 'This sentence can not

STRCOMMAND = STRCOMMAND VBCRLF

STRCOMMAND = STRCOMMAND & "Host:" & Host & VBCRLF

If Dir (SaveFileName) <> "" "" ""

DIM Confirm

Confirm = msgbox ("There is already a file, whether it is broken?", Vbyesno Vbquestion, "Tips")

IF confirm = Vbyes Then

DownPosition = ""

IF not OfileCtrl.readKeyFromini ("Update", "Downsize", AppPath "Update.ini", DownPosition THEN

'Read the number of bytes downloaded last time

MSGBOX "Read size error", vbinformation, "prompt" endiff

'Send a breakdown

STRCOMMAND = STRCOMMAND & "Range: Bytes =" & ClNG (DownPosition) & "-" & VBCRLF

Else

Kill SaveFileName 'Delete the original file

END IF

END IF

STRCOMMAND = STRCOMMAND & "Connection: Keep-alive" & vbcrlf

STRCOMMAND = STRCOMMAND & VBCRLF

.Senddata strcommand

End with

IF Err THEN

LBLPROCESSRESULT.CAPTION = LBLPROCESSRESULT.CAPTION & VBCRLF & VBCAPTION & VBCRLF & VBCAPTION & VBCRLF & VBCRLF & "Download File error:" & Err.Description

LBLPROCESSRESULT.REFRESH

END IF

End Sub

Step 2, specific implementation code Step 2: Receive data

'------------------------------------- -------------------------------

'Name: Winsck_DataArrival

'Author: REKER 2004/3/20

'DESC:

'Params: 略

'Return: None

'History: None

'------------------------------------- -------------------------------

Private sub winsck_dataarrival (byval Bytestotal As Long)

ON Error ResMe next

'Doevents: doevents

Dim Bytedata () AS Byte

Winsck.getdata Bytedata (), VBBYTE

ReceiveData = ReceiveData & strconv (Bytedata (), Vbunicore

If INSTR (1, ReceiveData, "Content-length:")> 0 and filesize = 0 Then 'only the first calculation, FileSize = 0

DIM POS1 AS Long, Pos2 As Long

POS1 = INSTR (1, ReceiveData, "Content-Length:")

POS2 = INSTR (POS1 16, ReceiveData, VBCRLF)

IF Pos2> Pos1 Then

FileSizeByte = MID (ReceiveData, POS1 16, POS2 - POS1 - 16) 'calculating the length of the file

STARTTIME = Timer () 'Save the time to start downloading

Progssbar.max = filesizebyte 'Setting progress bar

FileSize = FormatNumber (FileSizebyTe / ​​1024, 2) 'in KB

SHOWINFO "Document Total" CSTR (FileSize) "KB ..." End IF

END IF

'The data to find the data returned from the server to find the start position of the download file

If FileHeaderlen = 0 THEN

For i = 0 to ubound (bytedata ()) - 3

IF BYTEDATA (I) = 13 and bytedata (i 1) = 10 and bytedata (i 2) = 13 and bytedata (i 3) = 10 THEN

STARTPOS = i 4 'Save the length of the file header

FileHeaderlen = StartPOS

EXIT for

END IF

'Doevents

Next I

END IF

FileSizeHavedown = bytestotal FileSizehavedown - FileHeaderlen

'Download the file length, you need to subtract the file header of the response

DBLDownloadSpeed ​​= FormatNumber (FormatNumber (FileSizehavedown / 1024, 2) / (FormatNumber ((TimatNumber), 4), 2)

If DBLDOWNLOADSPEED <> 0 THEN 'calculates the remaining download time

SrestTime = GetrestTime (ClNG ((FileSize - (FileSizeHaveDown / 1024) / dbldownloadSpeed)) 'This procedure is slightly, you can delete this code

LabrestTime.caption = "Remaining Time: o" Sresttime

LabrestTime.Refresh

END IF

LabDownloadSpeed.caption = CSTR (DBLDownloadSpeed) "KB / S"

LabDownloadSpeed.refresh

ProgssBar.Value = filesishavedown

'Write data

Fnum = freefile ()

Open Savefilename for binary lock write as #fnum

If Lof (Fnum)> 0 THEN

Seek #fnum, lof (fnum) 1

END IF

IF StartPos> 0 THEN

For i = startpos to ubound (Bytedata ())

Put #fnum,, Bytedata (i)

Next I

Else

Put #fnum,, Bytedata ()

END IF

Close #fnum

IF Err THEN

LBLPROCESSRESULT.CAPTION = LBLPROCESSRESULT.CAPTION & VBCRLF & Get Data Error: "& Err.Description

LBLPROCESSRESULT.REFRESH

END IF

End Sub

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

New Post(0)