//Download file C# chuẩn:
//==============================
public static string DownloadFile(string url, string folderName, string filename)
{
//sample: https://vnn-imgs-f.vgcloud.vn/2021/10/19/22/ngam-ve-than-phan-phu-nu-xua-va-nay-3-240x160.jpg
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebResponse resp;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Proxy = null;
req.UserAgent = "Mozilla/5.0";
//string tt = resp.ContentEncoding.ToUpperInvariant(); //GZIP or DEFLATE
req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
resp = (HttpWebResponse)req.GetResponse();
}
catch (Exception)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Proxy = null;
req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
resp = (HttpWebResponse)req.GetResponse();
}
using (resp)
{
string folderPath = folderName;
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
string filePath = folderName + ChuyenTuCoDauSangKoDau(HttpUtility.UrlDecode(filename, Encoding.UTF8));
using (FileStream outputFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
resp.GetResponseStream().CopyTo(outputFileStream);
}
}
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}
public static void Download111(string url, string filename)
{
using (WebDownload client = new WebDownload())
{
client.Headers["User-Agent"] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
client.DownloadFile(new Uri(url), filename);
}
}
private static string ChuyenTuCoDauSangKoDau(string strUrl)
{
string str = strUrl.Trim().ToLower();
while (str.LastIndexOf(" ") > 0)
str = str.Replace(" ", "");
return str.Replace(" ", "-").Replace("~", "").Replace("`", "").Replace("!", "").Replace("@", "").Replace("#", "").Replace("$", "").Replace("%", "").Replace("^", "").Replace("&", "-").Replace("=", "").Replace("(", "").Replace(")", "").Replace("+", "").Replace(",", "").Replace(">", "").Replace("<", "").Replace("'", "").Replace("đ", "d").Replace("á", "a").Replace("à", "a").Replace("ạ", "a").Replace("ả", "a").Replace("ã", "a").Replace("ă", "a").Replace("ắ", "a").Replace("ằ", "a").Replace("ặ", "a").Replace("ẳ", "a").Replace("ẵ", "a").Replace("â", "a").Replace("ấ", "a").Replace("ầ", "a").Replace("ậ", "a").Replace("ẩ", "a").Replace("ẫ", "a").Replace("ạ", "a").Replace("ê", "e").Replace("ế", "e").Replace("ề", "e").Replace("ể", "e").Replace("ễ", "e").Replace("ệ", "e").Replace("e", "e").Replace("é", "e").Replace("è", "e").Replace("ẹ", "e").Replace("ẻ", "e").Replace("ẽ", "e").Replace("i", "i").Replace("í", "i").Replace("ì", "i").Replace("ị", "i").Replace("ỉ", "i").Replace("ĩ", "i").Replace("o", "o").Replace("ó", "o").Replace("ò", "o").Replace("ọ", "o").Replace("ỏ", "o").Replace("õ", "o").Replace("ô", "o").Replace("ố", "o").Replace("ồ", "o").Replace("ộ", "o").Replace("ổ", "o").Replace("ỗ", "o").Replace("ơ", "o").Replace("ớ", "o").Replace("ờ", "o").Replace("ợ", "o").Replace("ở", "o").Replace("ỡ", "o").Replace("u", "u").Replace("ú", "u").Replace("ù", "u").Replace("ụ", "u").Replace("ủ", "u").Replace("ũ", "u").Replace("ư", "u").Replace("ứ", "u").Replace("ừ", "u").Replace("ự", "u").Replace("ử", "u").Replace("ữ", "u").Replace("y", "y").Replace("ý", "y").Replace("ỳ", "y").Replace("ỵ", "y").Replace("ỷ", "y").Replace("ỹ", "y").Replace("/", "-").Replace("?", "-").Replace("\"", "").Replace(":", "-").Replace(";", "-").Replace("--", "-");
}
//========= Class webdownload
public class WebDownload : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
if (request != null)
{
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
}
return request;
}
}