Inet1.Execute异步循环出错
VB编程,利用Inet1.Execute异步循环下载某网站的图片文件,循环时遇到Inet2.Execute提示“仍在执行上一请求”,如果加上“On Error Resume Next”,则只能得到最后一个图片文件。在异步循环的情况下,如何让Inet2.Execute执行完上一请求,再NEXT?该用什么语句来控制呢?
附上部分VB代码
- VB code
Dim pages As StringPrivate Sub Command1_Click()On Error Resume NextFor i = 1 To 100If Len(i) = 0 ThenMsgBox "请输入页数"ElseIf Len(i) = 1 Thenpages = "P00000" & iElseIf Len(i) = 2 Thenpages = "P0000" & iElseIf Len(i) = 3 Thenpages = "P000" & iElseIf Len(i) = 4 Thenpages = "P00" & iElseIf Len(i) = 5 Thenpages = "P0" & iElseIf Len(i) = 6 Thenpages ="P" & iEnd Ifdim url as stringurl = Text1.Text & pages & ".jpg" Inet2.Execute urlstr, "GET"NextEnd SubPrivate Sub Inet2_StateChanged(ByVal State As Integer)Select Case StateCase icResponseCompleted Dim path As Stringpath = "C:\" & pages & ".jpg" Open path For Binary Access Write As #2dim lengh as string lengh = Inet2.GetHeader("Content-Length") Dim sData As Variant sData = Inet2.GetChunk(0, icByteArray) Do While LenB(sData) > 0 Put #2, , sData '得到下一大块。 sData = Inet2.GetChunk(0, icByteArray) Loop Put #2, , sData Close #2 Text2.Text = pages & ".jpg" & "下载完毕" & "|" & lengh End SelectEnd Sub[解决办法]
使用 DownloadFileFromURL() API,这个是同步的。
[解决办法]
下载不会?
还是下载不完?
[解决办法]
这个要等前一个完成之后再弄下一个才不会出现。
要么,你可以用多个inet来获取。我做过一个这样的就是用了inet数组。定义个结构保存他们各自的数据。