我正在尝试使用 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
我收到此错误
40006: Wrong protocol or connection state for the requested transaction or request
我做错了什么?
最佳答案
你检查过这个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/