当前位置:编程学习 > VB >>

关于用Inet下载的问题!

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

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
补充:VB ,  网络编程
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,