Download file over HTTPS using .NET (dotnet)

后端 未结 3 1786
后悔当初
后悔当初 2021-01-16 00:41

I would like to download a file using VB.NET (preferably) or C# via HTTPS.

I have this code to download a file over plain HTTP:

Dim client As WebClie         


        
3条回答
  •  旧时难觅i
    2021-01-16 01:24

    You need to add a certificate validator:

    // You need to call it only once in a static constructor or multiple times there is no problem
    ServicePointManager.ServerCertificateValidationCallback = ValidateCertificate;
    
        private static bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }
    

    In VB:

    ServicePointManager.ServerCertificateValidationCallback = AddressOf ValidateCertificate
    Dim client As WebClient = New WebClient()
    '...
    'Your code
    
      Private Shared Function ValidateCertificate(sender As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) As Boolean
            return True
      End Function
    

提交回复
热议问题