Слишком много автоматических перенаправлений было предпринято на веб-клиенте? - PullRequest
0 голосов
/ 25 мая 2018

Два режима для загрузки файлов и ошибка: слишком много автоматических перенаправлений было предпринято на веб-клиенте?Я использую Visual Basic.net 2015

   ' First 
    Dim myWebClient As New WebClient()
    myWebClient.DownloadFile("http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_lotfac.zip", "x:\temp\D_lotfac.zip")

    ' Second
    My.Computer.Network.DownloadFile("http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_lotfac.zip", "x:\temp\D_lotfac.zip", "", "", True, 60000, True)

Ответы [ 2 ]

0 голосов
/ 25 мая 2018

Новый код ... но 0 байтов

Dim wr As HttpWebRequest = CType(WebRequest.Create("http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_lotfac.zip"), HttpWebRequest)
    wr.KeepAlive = True
    wr.AllowAutoRedirect = True
    wr.ServicePoint.Expect100Continue = False
    wr.CookieContainer = New CookieContainer
    wr.AutomaticDecompression = DecompressionMethods.GZip
    wr.Headers.Add("Accept-Encoding: Deflate, gzip")

    Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
    Dim str As Stream = ws.GetResponseStream()
    Dim inBuf(ws.ContentLength) As Byte
    Dim bytesToRead As Integer = CInt(inBuf.Length)
    Dim bytesRead As Integer = 0
    While bytesToRead > 0
        Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
        If n = 0 Then
            Exit While
        End If
        bytesRead += n
        bytesToRead -= n
    End While
    Dim fstr As New FileStream("x:\temp\D_lotfac.zip", FileMode.OpenOrCreate, FileAccess.Write)
    fstr.Write(inBuf, 0, bytesRead)
    str.Close()
    fstr.Close()
0 голосов
/ 25 мая 2018

Этот метод включает в себя проверку протокола 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
...