The HTTP protocol is a text format communication. Download file is binary data, how to handle two formats, not affected by the VB's arbitrary unicode conversion, this code provides an example.
Option ExplicitPrivate strURL As StringPrivate mstrFileName As String, mlngFileNum As LongPrivate mlngFileLen As Long, mlngCurByte As LongPrivate mblnOnlyLen As Boolean, mblnPutStart As BooleanPrivate Sub Form_Load () Text1.Text = "" Text2.Text = "" Label1.Caption = "Total file word Section: 0 "Label2.caption =" Downloaded bytes: 0 "command1.caption =" Start download "command2.caption =" End SubPrivate Sub Command1_Click () mblnonlylen = false DownFileEnd SubPrivate Sub Command2_Click () if Command2. CAPTION = "Cancel" THEN COMMAND2.CAPTION = "acquisition length" Closeall Else Mblnonlylen = true Label1.caption = "file total byte: 0" DOWNFILE END IFEEND SUBPRIVATE SUB DOWNFILE () DIM Server AS STRING, Port As Long Dim i as Long, J AS Long, S as string strurl = text1.text 'Preparing the downloaded file URL if Strurl = "" THEN MSGBOX "Please enter the URL!" J = INSTR (1, Strurl, "http: //", vbtextcompare) IF j> 0 THEN J = J 7 else j = 1 end if i = instr (j, strurl, "/") IF i> 0 THEN Server = MID (Strurl, J, I - J) Else Msgbox "invalid URL! "EXIT SUB END I = Instr (1, Server,": ") IF i> 0 THEN Port = Val (MID (Server, i 1)) Server = Left (Server, I - 1) Strurl = Replace (Strulll ":" & Port, ", 1) Else port = 80 end if mstrfilename = text2.text 'Download files in this storage location and file name if mstrfilename =" "Then mstrfilename = app.path &" / " &
MID $ (Strull, Instrrev (Strurl, "/") 1) Else I = Instrrev (MSTRFILENAME, "/") IF i> 0 THEN S = Left $ (MSTRFILENAME, I - 1) IF Dir (s, vbdirectory) = "" THEN MSGBOX "Please enter the file storage path!" End if else mstrfilename = app.path & "& mstrfilename end if end if mblnputstart = false label2.caption =" Downloaded bytes: 0 "Command1.enabled = false Command2.Caption = "cancel" With Winsock1 If .State <> sckClosed Then .Close .Protocol = sckTCPProtocol .RemoteHost = Server .RemotePort = Port .Connect End WithEnd SubPrivate Sub Form_Unload (cancel As Integer) CloseAllEnd Sub
Private Sub Winsock1_Connect () DIM S AS STRING S = "Get" & Strurl & "HTTP / 1.0" VBCRLF S = S & "Accept: * / *" & VBCRLF S = S & "Pragma: no-cache" & vbcrlf S = S & "Cache-Control: No-cache" & VBCRLF S = S & "Connection: Close" & vbcrlf & vbcrlf S = S & VBCRLF WINSOCK1.SENDDATA Send Subprivate Sub Closeall () IF Winsock1.State <> SCKCLOSED THEN Winsock1.Close Close #mlngFileNum Command1.Enabled = True Command2.Caption = "acquired length" End SubPrivate Sub Winsock1_DataArrival (ByVal bytesTotal As Long) Dim RevData () As Byte Dim a () As Byte, b () As String, c ( ) AS STRING DIM S AS STRING, I As Long, K As Long On Error Goto Fail if MblnputStart = False Then Winsock1.peekdata Revdata, VBarray or Vbbyte K = INSTRB (1, Revdata, Chrb (13) & chrb (10) & Chrb (13) & chrb (10))) IF K> 0 THEN WINSOCK1.GETDATA REVDATA, VBARRAY OR VBBYTE A = Leftb (Revdata, K - 1) Revdata = MIDB (Revdata, K 4) s = STRCO NV (a, vbunicode) B = Split (S, VBCRLF) IF INSTR (1, B (0), "200 OK", VbtextCompare) = 0 THEN GOTO FAIL for i = 1 to Ubound (b) c = Split (B (i), ":") SELECT CASE C (0) Case "Content-Length" mlngfilelen = ClNG (C (1)) Label1.caption = "file total byte:" &