Этот метод включает в себя проверку протокола TLS 1.2 сертификата сервера, если он запрашивается.
Основной метод: HTTP_Download()
:
Передайте методу URL ресурсаи локальный путь (только путь, а не имя файла), где хранить ресурс.
Возвращает логическое значение, где False
означает, что произошел сбой.
Imports System.IO
Imports System.Net
Imports System.Net.Security
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Dim URLResource As String = "http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_lotfac.zip"
Dim result As Boolean = HTTP_Download(URLResource, "x:\temp")
MessageBox.Show("Done!")
Public Function HTTP_Download(ResourceURL As String, DestinationPath As String) As Boolean
Dim CookieJar As New CookieContainer()
Dim ErrorStatus As Boolean = False
ServicePointManager.Expect100Continue = False
ServicePointManager.MaxServicePointIdleTime = 30000
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
ServicePointManager.ServerCertificateValidationCallback = AddressOf TlsValidationCallback
Dim httpRequest As HttpWebRequest = WebRequest.CreateHttp(ResourceURL)
Try
httpRequest.CookieContainer = CookieJar
httpRequest.AllowAutoRedirect = True
httpRequest.KeepAlive = True
httpRequest.ConnectionGroupName = Guid.NewGuid().ToString()
httpRequest.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 10; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0"
httpRequest.Accept = "ext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
httpRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.8,es-ES;q=0.5,en;q=0.3")
httpRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate;q=0.8")
httpRequest.Headers.Add(HttpRequestHeader.CacheControl, "no-cache")
Using httpResponse As HttpWebResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
Dim ResponseStream As Stream = httpResponse.GetResponseStream()
If httpResponse.StatusCode = HttpStatusCode.OK Then
DestinationPath = Path.Combine(DestinationPath, httpResponse.ResponseUri.Segments.Last())
End If
Try
Dim MemStream As New MemoryStream()
ResponseStream.CopyTo(MemStream)
MemStream.Position = 0
Using fileStream As FileStream = File.Create(DestinationPath)
Dim read As Integer
Dim buffer As Byte() = New Byte(132071) {}
Do
read = MemStream.Read(buffer, 0, buffer.Length)
If read = 0 Then Exit Do
fileStream.Write(buffer, 0, read)
Loop While read <> 0
fileStream.Flush()
End Using
Catch dnf_ex As DirectoryNotFoundException
'Log report the File IO Exception
ErrorStatus = True
Throw
Catch ptl_ex As PathTooLongException
'Log report the File IO Exception
ErrorStatus = True
Throw
Catch io_ex As IOException
'Log report the File IO Exception
ErrorStatus = True
Throw
End Try
End Using
Catch exW As WebException
ErrorStatus = True
'Log report the WebException
Catch exS As Exception
ErrorStatus = True
'Log report the System Exception
Finally
If Not ErrorStatus Then
'Final processing if needed
End If
End Try
Return (ErrorStatus = False)
End Function
Private Function TlsValidationCallback(sender As Object, CACert As X509Certificate, CAChain As X509Chain, sslPolicyErrors As SslPolicyErrors) As Boolean
If (sslPolicyErrors = sslPolicyErrors.None) Then Return True
Dim _Certificate As New X509Certificate2(CACert)
CAChain.Build(_Certificate)
For Each CACStatus As X509ChainStatus In CAChain.ChainStatus
If (CACStatus.Status <> X509ChainStatusFlags.NoError) And
(CACStatus.Status <> X509ChainStatusFlags.UntrustedRoot) Then
Return False
End If
Next
Return True
End Function