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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| Imports System.Net
Imports System.IO
Public Class FileDownloader
Private _Url As String = String.Empty
Private _DirectoryPath As String = String.Empty
Public _IsStarted As Boolean = False
Public Delegate Sub _DownloadStarting(ByVal thread As FileDownloader)
Public Event DownloadStarting As _DownloadStarting
Public Delegate Sub ProgressChangedEventHandler(ByVal thread As FileDownloader, ByVal e As DownloadProgressArgs)
Public Event DownloadProgressChanged As ProgressChangedEventHandler
Public Delegate Sub DownloadCompletedEventHandler(ByVal thread As FileDownloader, ByVal e As DownloadCompletedArgs)
Public Event DownloadCompleted As DownloadCompletedEventHandler
Protected Overridable Sub OnDownloadStarting(ByVal thread As FileDownloader)
RaiseEvent DownloadStarting(Me)
End Sub
Protected Overridable Sub OnDownloadProgress(ByVal thread As FileDownloader, ByVal e As DownloadProgressArgs)
RaiseEvent DownloadProgressChanged(Me, e)
End Sub
Protected Overridable Sub OnDownloadCompleted(ByVal thread As FileDownloader, ByVal e As DownloadCompletedArgs)
RaiseEvent DownloadCompleted(Me, e)
End Sub
Public Sub New(ByVal Url As String, ByVal directory As String)
_Url = Url
_DirectoryPath = directory
End Sub
Public Sub StartDownload()
If String.IsNullOrEmpty(_Url) Then
Throw New ArgumentException("Veuillez fournir un URL.")
End If
If String.IsNullOrEmpty(_DirectoryPath) Then
Throw New ArgumentException("Veuillez fournir une destination.")
End If
_IsStarted = True
OnDownloadStarting(Me)
Try
Dim request As HttpWebRequest = CType(WebRequest.Create(New Uri(_Url)), HttpWebRequest)
With request
.Referer = _Url.ToString
.Method = "GET"
.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0"
.ContentType = "application/octet-stream"
.Accept = "*/*"
.Timeout = 3000
.AllowAutoRedirect = True
End With
Using response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
If response.StatusCode = HttpStatusCode.OK Then
Dim _ContentLenght As Integer = response.ContentLength
If _ContentLenght <= 0 Then Throw New Exception("Impossible d'obtenir la taille du fichier.")
Using stream As Stream = response.GetResponseStream()
Using fs As New FileStream(_DirectoryPath, FileMode.Create, FileAccess.Write, FileShare.Write)
Dim buffer As Byte() = New Byte(2048 - 1) {}
Dim _TotalBytesReceived As Integer = 0
Dim readsize As Integer = stream.Read(buffer, 0, buffer.Length)
While readsize > 0
fs.Write(buffer, 0, readsize)
_TotalBytesReceived += readsize
OnDownloadProgress(Me, New DownloadProgressArgs(_TotalBytesReceived, _ContentLenght))
Return
End While
End Using
End Using
Else
Throw New Exception("Échec de l'état de retour du serveur, StatusCode: " & response.StatusCode)
End If
End Using
OnDownloadCompleted(Me, New DownloadCompletedArgs(False))
Catch ex As Exception
OnDownloadCompleted(Me, New DownloadCompletedArgs(True))
End Try
End Sub
End Class
Public Class DownloadCompletedArgs : Inherits System.EventArgs
Private _Error As Boolean
Public Sub New(ByVal Err As Boolean)
_Error = Err
End Sub
Public ReadOnly Property [Error] As Boolean
Get
Return _Error
End Get
End Property
End Class
Public Class DownloadProgressArgs : Inherits System.EventArgs
Private _BytesReceived As Integer
Private _TotalBytesReceived As Integer
Public Sub New(ByVal BytesReceived As Integer, ByVal TotalBytesReceived As Integer)
_BytesReceived = BytesReceived
_TotalBytesReceived = TotalBytesReceived
End Sub
Public ReadOnly Property BytesReceived As Integer
Get
Return _BytesReceived
End Get
End Property
Public ReadOnly Property TotalBytesReceived As Integer
Get
Return _TotalBytesReceived
End Get
End Property
Public ReadOnly Property ProgressPercentage As Integer
Get
If _TotalBytesReceived > 0 Then
Return Math.Ceiling((_BytesReceived / _TotalBytesReceived) * 100)
Else
Return 0
End If
End Get
End Property
End Class |
Partager