question

MojtabaHakim-8125 avatar image
0 Votes"
MojtabaHakim-8125 asked JackJJun-MSFT edited

How to download a file from http and https in C# on windows server 2012 and windows 7

I'm trying to download this file AnyDesk.zip from a URL in windows server 2012 and windows 7

CS Code :
using System;
using System.Net;

 namespace ConsoleApp3
 {
     class Program
     {
         public static string saveLoc = @"AnyDesk.zip";
         static void Main(string[] args)
         {
             //1-//First Try---------------------------------------------
             using (WebClient wc = new WebClient())
             {
    
                 wc.DownloadFile(
                     // Param1 = Link of file
                     new System.Uri("https://ghaemcoarsh.com/AnyDesk.zip/"),
                     // Param2 = Path to save
                     @"AnyDesk.zip"
                 );
             }
    
             //2-//Secound Try---------------------------------------------
             ServicePointManager.Expect100Continue = true;
             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                    | SecurityProtocolType.Tls11
                    | SecurityProtocolType.Tls12
                    | SecurityProtocolType.Ssl3;
             string url = @"https://ghaemcoarsh.com/AnyDesk.zip/";
             using (WebClient wc = new WebClient())
             {
                 byte[] fileBytes = wc.DownloadData(url);
                 string fileType = wc.ResponseHeaders[HttpResponseHeader.ContentType];
                 System.IO.File.WriteAllBytes(saveLoc, fileBytes);
             }
    
             //3-//Third Try---------------------------------------------
             ServicePointManager.Expect100Continue = true;
             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                    | SecurityProtocolType.Tls11
                    | SecurityProtocolType.Tls12
                    | SecurityProtocolType.Ssl3;
    
             HttpWebRequest request = 
             (HttpWebRequest)WebRequest.Create("https://ghaemcoarsh.com/AnyDesk.zip/");
             request.Method = "GET";
             var response = request.GetResponse();
             var contenttype = response.Headers[".zip"]; 
             var stream = response.GetResponseStream();
             Console.ReadKey();
         }
     }
 }

but its have error in windows 2012 :

The request was aborted: Could not create SSL/TLS secure channel

But in Windows 7 strangely the file download is incomplete

I tried all way that was in internet about this but its not working

please help me

dotnet-csharpdotnet-cli
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@MojtabaHakim-8125 , based on my test, I could download the zip file in windows server 2012 successfully by using the first method. I think it maybe related to your http link. You could try to change the link to check if it works well.

0 Votes 0 ·

I tried exactly this link in python it work correctly

0 Votes 0 ·

But not c#

0 Votes 0 ·
Show more comments

0 Answers