public static void OfflineDownload(string url, string path, CookieAwareWebClient client = null) { FileStream fileStream = null; OfflineDownloadTask task = null; try { var retried = false; retry: var request = WebRequest.Create(url); var httpWebRequest = request as HttpWebRequest; if (httpWebRequest != null) { httpWebRequest.Referer = url; httpWebRequest.ReadWriteTimeout = Timeout.Infinite; if (client != null) client.ProcessRequest(request); } request.Timeout = Timeout.Infinite; var response = request.GetResponse(); if (!retried && url.StartsWith("http://goo.im", true, CultureInfo.InvariantCulture) && response.ContentType == "text/html") { retried = true; Thread.Sleep(15000); goto retry; } var stream = response.GetResponseStream(); var disposition = response.Headers["Content-Disposition"] ?? string.Empty; var pos = disposition.IndexOf("filename=", StringComparison.Ordinal); long? fileLength; if (stream.CanSeek) fileLength = stream.Length; else try { fileLength = response.ContentLength; } catch { fileLength = null; } if (fileLength < 0) fileLength = null; var fileName = (pos >= 0 ? disposition.Substring(pos + 9).Trim('"', '\'').UrlDecode() : GetFileName(url)).ToValidPath(); string mime, extension; try { mime = Helper.GetMime(response.ContentType); extension = Helper.GetDefaultExtension(mime); } catch { extension = Path.GetExtension(fileName); mime = Helper.GetDefaultExtension(extension); } if (Directory.Exists(FileHelper.GetFilePath(path))) { if (!string.IsNullOrEmpty(extension) && !fileName.EndsWith(extension, StringComparison.Ordinal)) fileName += extension; path = FileHelper.Combine(path, fileName); } task = new OfflineDownloadTask(url, path) { PID = Process.GetCurrentProcess().Id }; if (!string.IsNullOrWhiteSpace(mime)) task.Mime = mime; if (fileLength != null) task.FileLength = fileLength; task.Save(); stream.CopyTo(fileStream = File.Create(FileHelper.GetFilePath(path))); task.Finish(); } catch (Exception exc) { if (task == null) throw; task.ErrorMessage = exc.Message; task.Save(); if (client != null) throw; } finally { if (fileStream != null) fileStream.Close(); } }
public static void OfflineDownload(string url, string path, CookieAwareWebClient client = null) { FileStream fileStream = null; OfflineDownloadTask task = null; try { var retried = false; retry: var request = WebRequest.Create(url); var httpWebRequest = request as HttpWebRequest; if (httpWebRequest != null) { httpWebRequest.Referer = url; httpWebRequest.ReadWriteTimeout = Timeout.Infinite; if (client != null) { client.ProcessRequest(request); } } request.Timeout = Timeout.Infinite; var response = request.GetResponse(); if (!retried && url.StartsWith("http://goo.im", true, CultureInfo.InvariantCulture) && response.ContentType == "text/html") { retried = true; Thread.Sleep(15000); goto retry; } var stream = response.GetResponseStream(); var disposition = response.Headers["Content-Disposition"] ?? string.Empty; var pos = disposition.IndexOf("filename=", StringComparison.Ordinal); long?fileLength; if (stream.CanSeek) { fileLength = stream.Length; } else { try { fileLength = response.ContentLength; } catch { fileLength = null; } } if (fileLength < 0) { fileLength = null; } var fileName = (pos >= 0 ? disposition.Substring(pos + 9).Trim('"', '\'').UrlDecode() : GetFileName(url)).ToValidPath(); string mime, extension; try { mime = Helper.GetMime(response.ContentType); extension = Helper.GetDefaultExtension(mime); } catch { extension = Path.GetExtension(fileName); mime = Helper.GetDefaultExtension(extension); } if (Directory.Exists(FileHelper.GetFilePath(path))) { if (!string.IsNullOrEmpty(extension) && !fileName.EndsWith(extension, StringComparison.Ordinal)) { fileName += extension; } path = FileHelper.Combine(path, fileName); } task = new OfflineDownloadTask(url, path) { PID = Process.GetCurrentProcess().Id }; if (!string.IsNullOrWhiteSpace(mime)) { task.Mime = mime; } if (fileLength != null) { task.FileLength = fileLength; } task.Save(); stream.CopyTo(fileStream = File.Create(FileHelper.GetFilePath(path))); task.Finish(); } catch (Exception exc) { if (task == null) { throw; } task.ErrorMessage = exc.Message; task.Save(); if (client != null) { throw; } } finally { if (fileStream != null) { fileStream.Close(); } } }