关于用Inet下载的问题!

lianghengchao 2013-04-01 06:24:51
第一次打开下载速度正常!第二次打开下载速度明显变慢!还有就是直接关掉程序会出错的...各位高手帮看下吧!谢谢了!!直接上代码...

Dim x As Single '当前下载的大小
Dim y As Single '用掉的时间
Dim bDone As Boolean '停止下载的变量
Dim dx As String '文件大小
Dim yx As String '已下大小
Dim sd As String '下载速度

Private Sub StartDownLoad(ByVal Geturl As String)
Dim spo%, filename$
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(App.Path & "\download") Then Set f = fso.CreateFolder(App.Path & "\download")
spo = InStrRev(Geturl, "/")
filename = Right(Geturl, Len(Geturl) - spo) '获取文件名
Text2.Text = App.Path & "\download\" & filename
Inet1.Execute Geturl, "get" '开始下载
End Sub

Private Sub Command2_Click()
Command1.Enabled = True
Command2.Enabled = False
Timer1.Enabled = False
sd = "下载速度:" & "0.00" & "KB/s"
Inet1.Cancel
bDone = True
'Inet1.Execute , "close"
'Inet1.Execute , "quit"
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
'On Error Resume Next
'State = 12 时,用 GetChunk 方法检索服务器的响应。
Dim vtdata() As Byte
Select Case State
Case icHostResolvingHost
Label3.Caption = "正在查询所指定的主机的 IP 地址"
Case icHostResolved
Label3.Caption = "成功地找到所指定的主机的 IP 地址"
Case icConnecting
Label3.Caption = "正在与主机连接"
Case icConnected
Label3.Caption = "已与主机连接成功"
Case icRequesting
Label3.Caption = "正在向主机发送请求"
Case icRequestSent
Label3.Caption = "发送请求已成功"
Case icReceivingResponse
Label3.Caption = "在接收主机的响应"
Case icResponseReceived
Label3.Caption = "成功地接收到主机的响应"
Case icDisconnecting
Label3.Caption = "正在解除与主机的连接"
Case icDisconnected
Label3.Caption = "已成功地与主机解除了连接"
Case icError
Label3.Caption = "与主机通讯时出现了错误"
'出现错误时,返回 ResponseCode 和 ResponseInfo。
vtdata = Inet1.ResponseCode & ":" & Inet1.ResponseInfo
Case icResponseCompleted ' 12

'取得第一个块。
vtdata() = Inet1.GetChunk(1024, 1)
DoEvents
Open Text2.Text For Binary Access Write As #1 '设置保存路径文件后开始保存
'获取下载文件长度
If Len(Inet1.GetHeader("Content-Length")) > 0 Then ProgressBar1.Max = CLng(Inet1.GetHeader("Content-Length"))
dx = "文件大小:" & Format(Int(Inet1.GetHeader("Content-Length") / 1024 / 1024 * 10 ^ 2 + 0.5) / 10 ^ 2, "0.00") & "M"
'循环分块下载
Do While Not bDone

Put #1, Loc(1) + 1, vtdata()
vtdata() = Inet1.GetChunk(64, 1)
DoEvents
ProgressBar1.Value = Loc(1) '设置进度条长度
x = Loc(1)
yx = "已下载:" & Format(Int(Loc(1) / 1024 / 1024 * 10 ^ 2 + 0.5) / 10 ^ 2, "0.00") & "M"
Label4.Caption = dx & yx & sd
If Loc(1) >= ProgressBar1.Max Then bDone = True
Loop
Close #1
End Select
End Sub
Private Sub Command1_Click()
Command1.Enabled = False
Command2.Enabled = True
bDone = False
StartDownLoad Text1.Text
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
Text2.Text = App.Path
End Sub

Private Sub Timer1_Timer()
y = y + 1
sd = "下载速度:" & Format(Int(x / y / 1024 * 10 ^ 2 + 0.5) / 10 ^ 2, "0.00") & "KB/s"
End Sub
...全文
40 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

1,502

社区成员

发帖
与我相关
我的任务
社区描述
VB 网络编程
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧