Search code examples
vb.netbackgroundworkerparallel.foreach

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

this is part of my program. When I use this, it takes a long time for everyone to download. How can I download all the addresses at the same time?

I found the following code on the internet enter code here to do this, but I do not know how to use it in the my program. Please help. Thank you.

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

Solution

  • Take a look on this. Might be a good start (part of your code implemented on each method).

     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