//Download methods end //------------------------------------Cleanup methods private void DisposeWebClient() { try { cAWebClient.Dispose(); } catch { }//Already disposed or null }
protected virtual void Dispose(bool disposing) { if (disposing) { wc.Dispose(); } }
public void Dispose() { if (webclient != null) { webclient.Dispose(); } webclient = null; }
public override void Dispose() { if (_downloader != null) { RemoveHandlers(); _downloader.Dispose(); } base.Dispose(); }
private string postRequest(string url, NameValueCollection post = null, string referral = "") { //initialize client CookieAwareWebClient webClient = new CookieAwareWebClient(cookies); //Set webclient referrer if (referral != "") { webClient.Headers["Referer"] = referral; } //issue the request byte[] responseBytes = webClient.UploadValues(url, "POST", post); string result = Encoding.UTF8.GetString(responseBytes); //clear buffers and return result webClient.Dispose(); return(result); }
//web requests private string getRequest(string url, NameValueCollection get = null, string referral = "") { //initialize client CookieAwareWebClient webClient = new CookieAwareWebClient(cookies); //generate GET uri string request = url; if (get != null) { request += "?"; int it = 0; foreach (string s in get) { foreach (string v in get.GetValues(s)) { if (it != 0) { request += "&"; } request += s + "=" + v; it++; } } } //Set webclient referrer if (referral != "") { webClient.Headers["Referer"] = referral; } //issue the request and return output string output = webClient.DownloadString(request); webClient.Dispose(); return(output); }
public void SaveImageFromUri(string workingRootPath, string fileNamePath, ImageFormat format, String uri, String cookieName, String cookieValue) { CookieAwareWebClient client = new CookieAwareWebClient(); if (cookieName != null && cookieValue != null) { client.CookieContainer.Add(new Uri(uri), new Cookie(cookieName, cookieValue)); } Stream stream = client.OpenRead(uri); Bitmap bitmap; bitmap = new Bitmap(stream); if (bitmap != null) { DirectoryInfo directoryInfo = CreateFolder(workingRootPath, "validation"); bitmap.Save(fileNamePath, format); } stream.Flush(); stream.Close(); client.Dispose(); }
public static void Clear() { atualizador.Dispose(); _client.Dispose(); }
public void Dispose() { webClient.Dispose(); }
static void Main(string[] args) { Console.WriteLine("Starting."); string ConfigFile = "Default.xml"; if (args.Length > 0) { ConfigFile = args[0]; } try { XmlSerializer xml = new XmlSerializer(typeof(DownloadConfig)); TextReader XMLInput = new StreamReader(ConfigFile); Config = (DownloadConfig)xml.Deserialize(XMLInput); XMLInput.Close(); XMLInput.Dispose(); } catch { Console.WriteLine("Invalid Config file. Ending."); return; } int Page = 0; WebcastSummary[] result; List<WebcastSummary> WebcastSummaries = new List<WebcastSummary>(); do { result = WebcastSearch(Page++); if (result != null) WebcastSummaries.AddRange(result); } while (result != null && result.Length == 100); if (WebcastSummaries.Count == 0) { Console.WriteLine("Ending."); return; } foreach (WebcastSummary _summary in WebcastSummaries) { string aPath = Config.UseProxyMode == true ? Config.FilePath : Config.FilePath + @"/" + _summary.Code.Replace("/", "-"); string aCode = _summary.Code.Split('/')[1]; if (!Config.UseProxyMode && Directory.Exists(aPath) && Config.ReplaceIfExists) { Directory.Delete(aPath, true); } if (!Directory.Exists(aPath) || Config.UseProxyMode) { try { if (!Directory.Exists(aPath)) Directory.CreateDirectory(aPath); int WebcastGetResult; bool WebcastGetResultSpecified; Webcast Webcast; ServiceClient.WebcastGet( Config.Username, Config.Password, _summary.Code, _summary.Languages[0], out WebcastGetResult, out WebcastGetResultSpecified, out Webcast); Console.WriteLine("Retrieved " + Webcast.Code + "."); CookieAwareWebClient WebClient = new CookieAwareWebClient(); WebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); try { WebClient.UploadString(Webcast.RegisterUrl, "POST", "Username="******"&Password="******"/bb/") > 0) bbExists = true; } foreach (Attachment _attachment in Webcast.Attachments) { if ((_attachment.Location.EndsWith(".wmv") && (Config.RetrieveWebcastStreams || Config.UseProxyMode) && (_attachment.Location.IndexOf("/bb/") > 0 || _attachment.Location.IndexOf("/au/") > 0 || (!bbExists && _attachment.Location.IndexOf("/nb/") > 0) )) || (!_attachment.Location.EndsWith(".wmv") && Config.RetrieveWebcastAttachments && !Config.UseProxyMode)) { try { string fileName = Config.UseProxyMode ? aCode + ".wmv" : _attachment.Location.Substring(_attachment.Location.LastIndexOf("/") + 1); Console.WriteLine("Now downloading " + fileName + "."); WebClient.DownloadFile(_attachment.Location, aPath + @"/" + fileName); } catch { Console.WriteLine("Failed."); } } } } if (!Config.UseProxyMode && Config.RetrieveTopicAttachments) { foreach (Topic _topic in Webcast.Topics) { foreach (Attachment _attachment in _topic.Attachments) { try { Console.WriteLine("Now downloading " + _attachment.Location.Substring(_attachment.Location.LastIndexOf("/") + 1) + "."); WebClient.DownloadFile(_attachment.Location, aPath + @"/" + _attachment.Location.Substring(_attachment.Location.LastIndexOf("/") + 1)); } catch { Console.WriteLine("Failed."); } } } } if (!Config.UseProxyMode && Config.RetrieveSlideAttachments) { foreach (Slide _slide in Webcast.Slides) { foreach (Attachment _attachment in _slide.Attachments) { try { string fileName = _attachment.Location.Substring(_attachment.Location.LastIndexOf("/", _attachment.Location.LastIndexOf("/") - 1) + 1).Replace("/","-"); Console.WriteLine("Now downloading " + fileName + "."); WebClient.DownloadFile(_attachment.Location, aPath + @"/" + fileName); } catch { Console.WriteLine("Failed."); } } } } } catch { Console.WriteLine("Authorization failed, skipping this Webcast."); } if (!Config.UseProxyMode && Config.ExportWebcastXML) { try { Console.WriteLine("Writing XML."); XmlSerializer x = new XmlSerializer(Webcast.GetType()); TextWriter XMLDestination = new StreamWriter(aPath + @"/webcast.xml"); x.Serialize(XMLDestination, Webcast); XMLDestination.Close(); XMLDestination.Dispose(); } catch { Console.WriteLine("Failed."); } } WebClient.Dispose(); } catch { Console.WriteLine("Unable to retrieve " + _summary.Code + "."); } } } }
static void UpdateDiagramasONS() { LogUpdate("#Início - Diagramas ONS", true); LogUpdate("cdre.org.br/ conectando...", true); IEnumerable <Row> diagramasONS = ScrapOnsDiagramasAsync().GetAwaiter().GetResult(); int totalItems = diagramasONS.Count(); int counter = 1; FileInfo jsonInfoFile = new FileInfo(Path.Combine(DiagramasDir, "info.json")); string jsonInfo = File.Exists(jsonInfoFile.FullName) ? File.ReadAllText(jsonInfoFile.FullName) : null; IEnumerable <Row> localDiagramas = new List <Row>(); try //problema ao corromper gravação do json //parse arquivo json inválido { localDiagramas = JsonConvert.DeserializeObject <IEnumerable <Row> >(jsonInfo); } catch (Exception) { } if (LocalBookmarks.Count == 0) { localDiagramas = new List <Row>(); //forçar update completo caso bookmark corrompido } bool bookmarkUpdate = false; var client = new CookieAwareWebClient(); foreach (var diagrama in diagramasONS) { LogUpdate($"{diagrama.FileLeafRef} atualizando {counter++} de {totalItems}", true); string diagramaLink = $"https://cdre.ons.org.br{diagrama.FileRef}"; FileInfo diagramaFile = new FileInfo(Path.Combine(DiagramasDir, diagrama.FileLeafRef)); string revisao = localDiagramas.Where(w => w.FileLeafRef == diagrama.FileLeafRef).Select(s => s.Modified).FirstOrDefault(); if (revisao == diagrama.Modified && diagramaFile.Exists) { LogUpdate($"{diagrama.FileLeafRef} já se encontra na revisão vigente"); continue; } try { client.DownloadFile(diagramaLink, diagramaFile.FullName); bookmarkUpdate = true; var listBookmarks = GetPdfBookmark(diagramaFile, true, diagrama.MpoAssunto); if (LocalBookmarks.Any(w => w.MpoCodigo == diagrama.FileLeafRef)) { foreach (var item in LocalBookmarks) { if (item.MpoCodigo == diagrama.FileLeafRef) { item.Bookmarks = listBookmarks; } } } else { LocalBookmarks.Add(new ModelSearchBookmark { MpoCodigo = diagrama.FileLeafRef, Bookmarks = listBookmarks }); } LogUpdate($"{diagrama.FileLeafRef} atualizado da modificação {revisao} para modificação {diagrama.Modified} em {diagramaFile.FullName}"); } catch (Exception) { LogUpdate($"{diagrama.FileLeafRef} não foi possível atualização pelo link {diagramaLink}"); } } if (bookmarkUpdate) { File.WriteAllText(jsonBookmarkFile.FullName, JsonConvert.SerializeObject(LocalBookmarks)); } var diagramasVigentes = diagramasONS.Select(s => $"{jsonInfoFile.Directory.FullName}\\{s.FileLeafRef}"); var diagramasLocal = Directory.GetFiles(jsonInfoFile.Directory.FullName, "*.pdf").Except(diagramasVigentes); foreach (var item in diagramasLocal) { File.Delete(item); LogUpdate($"{Path.GetFileNameWithoutExtension(item)} não está vigente e foi apagado"); } File.WriteAllText(jsonInfoFile.FullName, JsonConvert.SerializeObject(diagramasONS)); client.Dispose(); LogUpdate("#Término - Diagramas ONS", true); }