본문 바로가기
프로그래밍/C#

[C#] URL에서 파일 바로 다운로드 받기

by 왕초보 개발자 2021. 3. 29.
728x90
// SSL/TLS 오류 해결을 위한 코드
System.Net.ServicePointManager.Expect100Continue = true;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3 | System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

System.Net.WebClient webClient = new System.Net.WebClient();

// 파일 다운로드받을 URL
String fileLink = "https://pub.insure.or.kr/compareDis/variableInsrn/prodFund/excelDownloadProfitView.do?prodCd=L011110012700&search_stdYmd=2021-03-29&memberCd=L01";

// 다운로드 받을 폴더/파일 지정해서 다운로드 
// **파일명 지정 안하면 오류 발생함
// **확장자가 URL에 업로드된 파일과 다르면 파일이 깨져서 다운로드됨
webClient.DownloadFile(new Uri(fileLink), @"C:\test.xls");

 

728x90