我正在尝试使用 Winsock 下载一些文件并保存它们。
就我而言,我有一个包含 2 列的 MSHFlexGrid:一列带有 URL,另一列带有“路径+文件名”(文件将被保存的位置)。
我正在遍历调用下一个函数的所有行:
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
我收到这个错误
我究竟做错了什么?
最佳答案
你检查过这个 Microsoft Support page 吗?这表明 Winsock 控件中存在错误,修补程序可能会有所帮助。
要尝试的另一件事是在尝试读取/发送数据之前确保您的 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
最后一件事。查看 this post 使用 Winsock 控件。
关于vb6 - Winsock 下载文件 - vb6,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1517047/