How to Download File using HTTP - VB.net

How to Download File using HTTP - VB.net

Private Sub DownloadFile()
  Dim strURL As String = IO.Path.Combine(txtUpdateURL.Text, UpdateZipFileName).Replace("\", "/")
                strDownloadFilePath = IO.Path.Combine(txtUpdateDownloadPath.Text, UpdateZipFileName)
                If File.Exists(strDownloadFilePath) = True Then
                    File.Delete(strDownloadFilePath)  ' ZIp File                
                    System.Threading.Thread.Sleep(100)
                End If
                ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications
                ServicePointManager.SecurityProtocol = CType(768, SecurityProtocolType) Or CType(3072, SecurityProtocolType)
                Dim client As New System.Net.WebClient
                client.UseDefaultCredentials = False
                Dim creds As New CredentialCache()
                creds.Add(New Uri(strURL), "Basic", New NetworkCredential("arc-10055", "Pass@12345", "www.arc-india.com"))
                'client.Credentials = creds
                client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;")
                AddHandler client.DownloadProgressChanged, AddressOf DownloadProgressCallback4
                AddHandler client.DownloadFileCompleted, AddressOf DownloadDataCompleted
                client.DownloadFileAsync(New Uri(strURL), strDownloadFilePath)
End Sub


Public Function AcceptAllCertifications(ByVal sender As Object, ByVal certification As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean
        Return True
    End Function



 Private Sub DownloadProgressCallback4(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
        ' Displays the operation identifier, and the transfer progress.
        'Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...", CStr(e.UserState), e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage)
        lblProgressBarStatus.Text = "Downloading " & e.ProgressPercentage.ToString & " % complete..."
        ProgressBar1.Value = e.ProgressPercentage
    End Sub
    Private Sub DownloadDataCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
        Try
            If IO.File.Exists(strDownloadFilePath) = True Then
                Console.WriteLine("Completed")
                Application.DoEvents()
                System.Threading.Thread.Sleep(100)
            End If
        Catch ex As Exception
          MessageBox.Show(ex.Message, "Download Completed Failed " & ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
          
        End Try
    End Sub

Comments

Popular posts from this blog

Shared / Static Class in vb.net/C#

Xamarin Forms : PopUp Page with RG.Plugins.Popup

Text was truncated or one or more characters had no match in the target code page.". (SQL Server Import and Export Wizard)