1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| Private Sub DownloaderVideo(URL As String, Names As String, destination As String, Total As Integer)
Task.Factory.StartNew(Sub()
Try
Dim request As HttpWebRequest = CType(WebRequest.Create(New Uri(URL)), HttpWebRequest)
With request
.Method = "GET"
.Timeout = 20000
.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0"
End With
Using response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
If response.StatusCode = HttpStatusCode.OK Then
Dim counter As Integer = 1
Dim _ContentLenght As Integer = response.ContentLength
Using stream As Stream = response.GetResponseStream()
Using fs As New FileStream(Path.Combine(destination, Names & Path.GetExtension(URL)), FileMode.Create, FileAccess.Write, FileShare.Write)
Dim _TotalBytesReceived As Integer = 0
Dim bufferSize As Long = 4096
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
Dim isRead As Boolean = True
Do
Dim readedBytes As Integer = stream.Read(buffer, 0, buffer.Length)
If readedBytes = 0 Then
isRead = False
Continue Do
End If
_TotalBytesReceived += readedBytes
fs.Write(buffer, 0, readedBytes)
Me.BeginInvoke(New TotalInfoDelegate(AddressOf SetInfoVideo), counter, Total) '<= total de vidéo télécharger
Me.BeginInvoke(New ResponseInfoDelegate(AddressOf SetResponseInfo), Names, _TotalBytesReceived, _ContentLenght)
Loop While isRead
counter += 1
End Using
End Using
End If
End Using
Me.Invoke(New DoneDelegate(AddressOf DownloadValide), True) '<= une fois finie de télécharger
Catch ex As WebException
Me.Invoke(New DoneDelegate(AddressOf DownloadValide), False) '<= affiche une erreur avant de télécharger
End Try
End Sub)
End Sub |
Partager