Winsock uploads files - vb6

I am trying to use Winsock to download some files and save them. In my case, I have an MSHFlexGrid with two columns: one with a URL and the other with "path + filename" (where the file will be saved). I repeat all the lines calling the following function:

Public Function DownloadSock(ArqURL As String, ArqDestino As String) As Boolean
'ArqURL is the file URL
'ArqDestino is where the downloaded file is going to be stored, in my hard disc

Dim arquivo() As Byte
Dim ficheiroID As Integer

ficheiroID = FreeFile
On Error GoTo Trata_erro
Open ArqDestino For Binary Access Write As #ficheiroID


Me.Winsock1.Connect ArqURL, 80 
Me.Winsock1.GetData arquivo()
Put #ficheiroID, , arquivo()

Close #ficheiroID

DownloadSock = True


Exit Function

Trata_erro:

    MDIForm1.Text1 = MDIForm1.Text1 & "Error! " & Err.Number & Err.Description & " - " & Err.Source & " - URL: " & ArqURL & " - Destino: " & ArqDestino & vbNewLine
    DownloadSock = False

End Function

I get this error

40006: wrong protocol or connection for requested transaction or request

What am I doing wrong?

thanks

+3
source share
2 answers

Have you checked this Microsoft support page ? It indicates an error in the Winsock control and a correction may be helpful.

, - , winsock , / , , :

if winsock.state=9 ' error state
  winsock.close
  while winsock.state<>0 ' closed state
    doEvents
  wend ' you need a while loop, because it doesn't close "immediately".
end if
' now you reopen it, or do whatever else you need

- :

With Winsock1
      If .State <> sckClosed Then .Close
      .RemoteHost = ArqURL 
      .RemotePort = 80
      .Connect
End With

. Winsock.

+2

, Winsock. Winsock GetData . . , Winsock DataArrival, GetData . :

Public Sub DownloadSock(ArqURL As String)

  Dim arquivo() As Byte
  Dim ficheiroID As Integer

  ficheiroID = FreeFile
  Me.Winsock1.Connect ArqURL, 80

End Function

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

  Dim ArqDestino As String
  Dim arquivo() As Byte
  Dim ficheiroID As Integer

  ficheiroID = FreeFile
  Open ArqDestino For Binary Access Write As #ficheiroID
  Me.Winsock1.GetData arquivo()
  Put #ficheiroID, , arquivo()
  Close #ficheiroID

End Sub

( , , ). - , . , DataArrival, , . , .

/ ( VB6, Winsock CodeProject, ).

+1

Source: https://habr.com/ru/post/1719226/


All Articles