1. ホーム
  2. vb.net

[解決済み] 複数のURLを同時にダウンロードするには?

2022-03-04 18:54:19

質問

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

これは私のプログラムの一部です。 これを使うと、全員がダウンロードするのに長い時間がかかります。どうすればすべてのアドレスを同時にダウンロードできるのでしょうか?

インターネット上で次のようなコードを見つけましたが、私のプログラムでどのように使用すればよいのかわかりません。助けてください。ありがとうございます。

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