如何知道winsock已经完成接收文件?
我使用的是 Winsock
控件:
Private Sub Form_Load()
Winsock1.Connect "stackoverflow.com", 80
End Sub
Private Sub Winsock1_Close()
Winsock1.Close
End Sub
Private Sub Winsock1_Connect()
Winsock1.SendData "GET /questions/8624871/vb6-alternative-to-inet-webbrowser-control HTTP/1.1" & vbCrLf & "Host: stackoverflow.com" & vbCrLf & vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim s As String
Winsock1.GetData s, vbString
RichTextBox1.Text = RichTextBox1.Text & s
End Sub
当标头不包含 Content-Length
时,如何知道控件已完成接收文件?
我听说过一些方法,例如当 Winsock1.state
为 0
时,这意味着连接已关闭,但有时它仍处于其他状态,例如 7
,所以我需要另一个解决方案。
I'm using the Winsock
control:
Private Sub Form_Load()
Winsock1.Connect "stackoverflow.com", 80
End Sub
Private Sub Winsock1_Close()
Winsock1.Close
End Sub
Private Sub Winsock1_Connect()
Winsock1.SendData "GET /questions/8624871/vb6-alternative-to-inet-webbrowser-control HTTP/1.1" & vbCrLf & "Host: stackoverflow.com" & vbCrLf & vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim s As String
Winsock1.GetData s, vbString
RichTextBox1.Text = RichTextBox1.Text & s
End Sub
How can I know that the control has completed receiving the file when the header doesn't contain Content-Length
?
I've heard of some ways, like when Winsock1.state
is 0
it means that the connection is closed, but sometimes it remains in some other state, like 7
, so I need another solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要解析收到的 HTTP 标头。 RFC 2616 第 4.4 节 解释了它们如何告诉您数据的长度以及如何读取数据。
You need to parse the received HTTP headers. RFC 2616 Section 4.4 explains how they tell you the length of the data and how the data needs to be read.
您可以在 VB6 中使用许多组件来发出 HTTP 请求。有些是 VB6 附带的,有些现在是 Windows 的一部分。其中大多数比使用 Winsock 控件在 TCP 之上滚动您自己的 HTTP 好得多。标头处理只是他们可以帮助您的事情之一。
There are a number of components you can use in VB6 for making HTTP requests. Some come with VB6, some are part of Windows now. Most of these are far better than rolling your own HTTP on top of TCP using the Winsock control. Header processing is just one of the things they can assist you in.
如果您仍然想开发自己的 HTTP 客户端,则应该阅读 HTTP 规范而不是猜测。
HTTP 传输使用 Content-Length 标头来确定数据的长度。如果标头丢失,则连接关闭将指示数据结束。
If you still want to roll your own HTTP client, you should read the HTTP spec rather than guessing.
HTTP transfers use the Content-Length header to determine the length of the data. If the header is missing, then the end of data is signalled by the connection closing.