在webclient上尝试了太多的自动重定向?

时间:2018-05-24 21:26:29

标签: vb.net webclient

下载文件的两种模式和错误:在webclient上尝试了太多的自动重定向? 我使用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 :(得分:0)

此方法包括服务器证书的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

答案 1 :(得分:-1)

新代码......但是0字节

pthread_t thread[ARRAY_SIZE];


void* mmult(void* r){
   int row = (intptr_t) r;         
   int index;                                
   int column, tmp, size = ARRAY_SIZE;
   for(column = 0; column < size; column++){ 
      tmp = 0;
      for(index = 0; index < size; index++){
         tmp += MA[row * size + index]  *  MBT[column * size + index];
      }
      MC[row * size + column] = tmp;
   }

   pthread_exit(NULL);      


}
int main(void) {
   int size = ARRAY_SIZE, row, column;
   struct timeval start, end;
   double exectime;

   init_matrix(MA); //function to create Matrix
   init_matrix(MB);



   for(row = 0; row < size; row++){ /* create Array-Threads */

      if(pthread_create(&thread[row], NULL, &mmult,&row) != 0){ //also doesnt work with (void*) &row
         perror("Fehler beim Erstellen eines Feld-Threads");
         exit(EXIT_FAILURE);
      }
    }

   int i;
   for(i =0; row < size; i++){
      status = pthread_join(thread[i], NULL);
   }


   return 0;
}