下载网页中链接的文件

来源:互联网 发布:三国志13帧数优化 3dm 编辑:程序博客网 时间:2024/05/01 18:01

   

Dim th As New System.Threading.Thread(AddressOf download)
    Sub reload()
        Dim url As New System.Net.WebClient
        Dim b() As Byte = url.DownloadData(TextBox1.Text)
        Dim s As String = System.Text.Encoding.Default.GetString(b)
        TextBox4.Text = s
        Dim base As String = TextBox1.Text
        base = base.Substring(0, base.LastIndexOf("/") + 1)
        Dim ss1() As String = Split(TextBox2.Text, " ")
        Dim i, j As Integer
        For i = 0 To ss1.Length - 1
            Dim ss() As String = getSRC(s, ss1(i), base)
            For j = 0 To ss.Length - 1
                CheckedListBox1.Items.Add(ss(j), True)
            Next
        Next
    End Sub
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Chr(13) Then
            reload()
        End If
    End Sub
    Function getSRC(ByVal s As String, ByVal hz As String, ByVal base As String) As String()
        Dim ss() As String = Split(s, hz)
        Dim sss(ss.Length - 2) As String
        Dim i, j As Integer
        For i = 0 To ss.Length - 2
            j = ss(i).LastIndexOf(" ")
            If j < 0 Then j = 0
            Dim src As String = ss(i).Substring(j, ss(i).Length - j) & hz
            j = src.IndexOf("http://")
            Dim b As String = ""
            If j <= 0 Then
                j = src.LastIndexOf("=") + 1
                b = base
            End If
            If j <= 0 Then
                j = src.LastIndexOf("('") + 2
                b = base
            End If
            src = b & src.Substring(j, src.Length - j)
            src = src.Replace("'", "")
            src = src.Replace(Chr(34), "")
            sss(i) = src
        Next
        Return sss
    End Function

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        If e.KeyChar = Chr(13) Then
            reload()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "Download" Then
            th.Start()
            Button1.Text = "Stop"
        Else
            Timer1.Enabled = True
            Button1.Enabled = False
            Button1.Text = "Waiting..."
            th.Abort()
        End If
    End Sub
    Sub download()
        If CheckedListBox1.Items.Count = 0 Then
            reload()
        End If
        Dim url As New System.Net.WebClient
        Dim i As Integer
        For i = 0 To CheckedListBox1.Items.Count - 1
            If CheckedListBox1.GetItemCheckState(i) = CheckState.Checked Then
                Dim s As String = CheckedListBox1.Items.Item(i)
                Dim f As String = System.IO.Path.GetFileName(s)
                Try
                    Me.Text = s
                    url.DownloadFile(s, TextBox3.Text & "/" & f)
                    CheckedListBox1.SetItemCheckState(i, CheckState.Unchecked)
                Catch ex As Exception
                    TextBox4.Text &= ex.ToString & vbNewLine
                End Try
            End If

        Next
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If th.ThreadState = Threading.ThreadState.Stopped Then
            Button1.Text = "Download"
            Button1.Enabled = True
            Timer1.Enabled = False
        End If
    End Sub

原创粉丝点击