Imports System.Net
Imports System.Web
Public Class MyAsyncReader
Public Event DownloadComplete()
Public Event ProgressChanged(ByVal Progress As Integer)
Public Function DownloadFile(ByVal DownloadURL As String, ByVal LocalFile As String, ByVal CookieString As String) As Boolean
Try
'State object
Dim myStateClass As MyAsyncReaderClass = New MyAsyncReaderClass
'Get total file size
Dim myHTTPRequest As HttpWebRequest = CType(WebRequest.Create(DownloadURL), HttpWebRequest)
'myHTTPRequest.AllowAutoRedirect = True
'setup proxy
If MasterForm.UseProxy Then
Dim myProxy As New WebProxy
myProxy.Address = New Uri("http://" + MasterForm.ProxyHost + ":" + MasterForm.ProxyPort)
myProxy.Credentials = New NetworkCredential(MasterForm.ProxyLogin, MasterForm.ProxyPassword)
myHTTPRequest.Proxy = myProxy
End If
'set up cookies
myHTTPRequest.CookieContainer = New CookieContainer
myHTTPRequest.CookieContainer.SetCookies(New Uri("http://www.mydomain.com/"), CookieString)
Dim myHTTPResponse As HttpWebResponse = CType(myHTTPRequest.GetResponse(), HttpWebResponse)
myStateClass.TotalBytes = myHTTPResponse.ContentLength
myStateClass.SavedBytes = 0
myHTTPResponse.Close()
myHTTPResponse = Nothing
myHTTPRequest = Nothing
'Open URL
Dim myWebClient As System.Net.WebClient = New System.Net.WebClient
Dim myStream As System.IO.Stream = myWebClient.OpenRead(DownloadURL)
'Open reading/writing streams
myStateClass.Writer = New System.IO.StreamWriter(LocalFile)
myStateClass.Reader = New System.IO.StreamReader(myStream)
'Start reading asynchronously
myStateClass.Reader.BaseStream.BeginRead(myStateClass.bBuffer, 0, 4096, New AsyncCallback(AddressOf myCallBackFunction), myStateClass)
Return True
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Downloading", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
'Async callback
Private Sub myCallBackFunction(ByVal asyncResult As IAsyncResult)
'Get state object
Dim myStateClass As MyAsyncReaderClass = CType(asyncResult.AsyncState, MyAsyncReaderClass)
Dim bytesRead As Integer = myStateClass.Reader.BaseStream.EndRead(asyncResult)
'Advance progress
myStateClass.SavedBytes += bytesRead
'Assuming a public progress bar named progBar with min value=0 and max value=100
MasterForm.ProgressStatusBar.progressBar.Value = Convert.ToInt32(myStateClass.SavedBytes * 100 / myStateClass.TotalBytes)
Dim DownloadedSoFar As Double = Math.Round(myStateClass.SavedBytes / 1024, 2)
Dim TotalBytes As Double = Math.Round(myStateClass.TotalBytes / 1024, 2)
MasterForm.ProgressStatusBar.Panels(1).Text = "Downloading... " + DownloadedSoFar.ToString + " KB of " + TotalBytes.ToString + " KB"
myStateClass.Writer.BaseStream.Write(myStateClass.bBuffer, 0, bytesRead)
If myStateClass.SavedBytes < myStateClass.TotalBytes Then
'Nope, do it again
myStateClass.Reader.BaseStream.BeginRead(myStateClass.bBuffer, 0, 4096, New AsyncCallback(AddressOf myCallBackFunction), myStateClass)
Else
'Yes, close everything
myStateClass.Writer.Close()
myStateClass.Reader.Close()
myStateClass.Writer = Nothing
myStateClass.Reader = Nothing
RaiseEvent DownloadComplete()
End If
End Sub
'Class to create state object
Private Class MyAsyncReaderClass
Public TotalBytes As Integer 'Total bytes of file
Public SavedBytes As Integer 'Bytes read so far
Public Writer As System.IO.StreamWriter 'Writing stream
Public Reader As System.IO.StreamReader 'Reading stream
Public bBuffer(4096) As Byte 'Buffer
End Class
End Class