vb6 - Winsock下载文件-vb6

标签 vb6 winsock download

我正在尝试使用 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/

相关文章:

javascript - 如何捕获 iframe 中下载的文件?

api - UDP 使用套接字 API

vb6 - 如何在 Visual Basic 6 中为每个列表项命名

c++ - 将 TCP 服务器转换为 UDP 服务器

javascript - 是否可以仅使用 JavaScript(客户端方法)在浏览器中针对可识别的 MIME 类型启动下载提示?

node.js - 从 S3 下载文件夹时中断功能

vb6 - VB中可变数量的参数

sql - Vb6 缺少运算符

c++ - 特定接口(interface)上的 TCP/IP 连接

c - 通过Winsock发送字符串到HDFS