Created
January 2, 2017 12:56
-
-
Save JKamsker/73dc6215e0d70d74d5f44144139e8327 to your computer and use it in GitHub Desktop.
Downloads a file into a string via GET - Cached
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string md5(string input, hashFlag flag = 0) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
Stream fs; | |
if (flag == hashFlag.hashFile) | |
if (File.Exists(input)) | |
fs = File.OpenRead(input); | |
else | |
throw new Exception("File does not exist"); | |
else | |
fs = new MemoryStream(Encoding.ASCII.GetBytes(input)); | |
foreach (var item in MD5.Create().ComputeHash(fs)) sb.Append(item.ToString("X2")); | |
return sb.ToString(); | |
} | |
public static string dlString(string url, bool cache = true) | |
{ | |
string urlSum = md5(url); | |
if (!Directory.Exists(cachePath)) | |
Directory.CreateDirectory(cachePath); | |
if (File.Exists(cachePath + "\\" + urlSum) && cache) | |
return File.ReadAllText(cachePath + "\\" + urlSum); | |
else | |
{ | |
WebClient wc = new WebClient(); | |
wc.Encoding = UTF8Encoding.UTF8; | |
wc.Proxy = null; | |
wc.Headers["Cache-Control"] = "max-age=0"; | |
wc.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; | |
wc.Headers["User-agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"; | |
wc.Headers["Accept-Language"] = "en-US,en;q=0.8"; | |
string webSite = wc.DownloadString(url); | |
File.WriteAllText(cachePath + "\\" + urlSum, webSite); | |
return webSite; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment