如何同时下载多个网址?

How to download multiple URLs at the same time?

Public Class Form1
    For i As Integer = 0 To RichTextBox1.Lines.Length - 1
        wreq=System.Net.WebRequest.Create("i  th Internet address")
        wreq.AutomaticDecompression = Net.DecompressionMethods.GZip
        wres = wreq.GetResponse
        Dim s As System.IO.Stream = wres.GetResponseStream
        Dim sr As New System.IO.StreamReader(s)
        html = sr.ReadToEnd
        s = html.Split(";")
        'here is other codes
    Next
End Class

这是我计划的一部分。 我用这个的时候,大家下载好久。如何同时下载所有地址?

我在网上找到了下面的代码enter code here to do this,但我不知道如何在我的程序中使用它。请帮忙。谢谢。

Imports System.Threading.Tasks
Imports System.Net
Imports System.IO
 
Public Class Form1
 
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Start a background task so as not to freeze up the UI.
        Me.BackgroundWorker1.RunWorkerAsync()
    End Sub
 
    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim files As String() 'Get file paths.
 
        'Download multiple files simultaneously.
        Parallel.ForEach(files,
                         Sub(f) Call New WebClient().DownloadFile(f,
                                                                  Path.Combine("local folder here",
                                                                               Path.GetFileName(f))))
    End Sub
 
    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        MessageBox.Show("All files downloaded")
    End Sub
 
End Class

看看这个。可能是一个好的开始(在每个方法上实现的部分代码)。

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim urls() As String = RichTextBox1.Lines.Select(Function(url) Trim(url))
        Parallel.ForEach(urls, Sub(f)
                                   If Not String.IsNullOrEmpty(f) Then
                                       DownloadAsync(f)
                                   End If
                               End Sub)
 End Sub



Function DownloadAsync(URL As String) As Task(Of Boolean)

    Try
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
        Dim html As String = ""
        Dim result As Boolean
        Dim request As HttpWebRequest = HttpWebRequest.Create(URL)
        request.AutomaticDecompression = DecompressionMethods.GZip
        request.Timeout = 500

        request.Method = "GET"
        request.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0"

        Using response As Task(Of WebResponse) = request.GetResponseAsync
            If response.Result IsNot Nothing Then
                Using ioStream As IO.Stream = response.Result.GetResponseStream
                    Using sr As New System.IO.StreamReader(ioStream)
                        html = sr.ReadToEnd
                        Dim s() As String = html.Split(";"c)
                        For Each sl In s
                            Debug.WriteLine(sl)
                        Next
                    End Using
                    result = True
                End Using
            End If
        End Using

        Return Task.FromResult(result)

    Catch ex As Exception
        Debug.WriteLine(ex.Message)
    End Try

    Return Task.FromResult(False)

End Function