This is an example program that demonstrates how to do an HTTP download in VB.NET with progress monitoring and abort capabilities.
A few notes:
- Declare the Chilkat.Http object WithEvents
- Don’t forget to set Chilkat.Http.EnableEvents = true, otherwise you won’t get callbacks.
- Use the dropdown box in Visual Studio to select your Chilkat.Http object, then select the OnPercentDone event. VB.NET will create the event callback function for you. You can then add your application code to the event callback.
Here is an example:
' Declare the Chilkat.Http object WithEvents
Dim WithEvents m_http As New Chilkat.Http
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' The unlock is only needed once at the beginning of the program.
' As long as UnlockComponent is called once, additional instances
' of Chilkat.Http can be created and used without needing to call UnlockComponent.
m_http.UnlockComponent("anything for 30-day trial")
' Don't forget to enable events...
m_http.EnableEvents = True
' Download Python for Windows
Dim success As Boolean
success = m_http.Download("http://www.python.org/ftp/python/2.5.1/python-2.5.1.msi", "python-2.5.1.msi")
If (success) Then
' This is the event callback. It is called periodically whenever the percentage completion
' increases. For short downloads, it will likely not be called once for each 1% increment.
' However, you should always get a final callback where args.PercentDone = 100,
' in the case of a successful download.
Private Sub m_http_OnPercentDone(ByVal sender As Object, ByVal args As Chilkat.PercentDoneEventArgs) Handles m_http.OnPercentDone
ProgressBar1.Value = args.PercentDone
' To abort the HTTP download at any point, simply set args.Abort = true
' (uncomment the following lines to abort at 50% completion)
'If (args.PercentDone >= 50) Then
' args.Abort = True