public Stream GetManufacturerImageStream() { HttpWebRequest request = Requests.CreateDefaultRequest(m_manufacturerImageLink); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return(response.GetResponseStream()); }
public static AllDataSheetSearchResult Search(string value) { string url = $"{SiteAddress}?Searchword={value}&sPage=1&sField=4"; HttpWebRequest request = Requests.CreateDefaultRequest(url); string result = Requests.ReadResponseString(request); HtmlDocument document = new HtmlDocument(); document.LoadHtml(result); int totalPages; AllDataSheetSearchContext.SearchOption option; FilterSearchMode(document, out totalPages, out option); AllDataSheetSearchContext searchContext = new AllDataSheetSearchContext(value); searchContext.NextPage = 1; searchContext.TotalPages = totalPages; if (option != AllDataSheetSearchContext.SearchOption.Match) { searchContext.UsedOptions.Add(AllDataSheetSearchContext.SearchOption.Match); searchContext.Option = option; } searchContext.Next(url); AllDataSheetSearchResult ret = new AllDataSheetSearchResult(); ret.Parts = FilterResults(document); ret.SearchContext = searchContext; return(ret); }
public static AllDataSheetSearchResult Search(AllDataSheetSearchContext searchContext) { if (!searchContext.CanLoadMore) { AllDataSheetSearchResult ret = new AllDataSheetSearchResult(); ret.Parts = null; ret.SearchContext = searchContext; return(ret); } while (true) { string url = $"{SiteAddress}?Searchword={searchContext.SearchValue}&sPage={searchContext.NextPage}&sField={(int)searchContext.Option}"; HttpWebRequest request = Requests.CreateDefaultRequest(url); request.Referer = searchContext.Referer; string result = Requests.ReadResponseString(request); HtmlDocument document = new HtmlDocument(); document.LoadHtml(result); if (searchContext.NextPage == 1) { int totalPages; AllDataSheetSearchContext.SearchOption option; FilterSearchMode(document, out totalPages, out option); searchContext.TotalPages = totalPages; if (searchContext.Option != option) { if (!searchContext.UsedOptions.Contains(searchContext.Option)) { searchContext.UsedOptions.Add(searchContext.Option); } searchContext.Option = option; } } AllDataSheetSearchResult sret = new AllDataSheetSearchResult(); if (searchContext.UsedOptions.Contains(searchContext.Option)) { sret.Parts = new List <AllDataSheetPart>(); if (searchContext.CanLoadMore) { searchContext.Next(url); continue; } } else { sret.Parts = FilterResults(document); } sret.SearchContext = searchContext; searchContext.Next(url); return(sret); } }
public async static Task <NewVersionInfo> RequestInfoAsync(string url) { NewVersionInfo info = new NewVersionInfo(); HttpWebRequest request = Requests.CreateDefaultRequest(url); string result = await Requests.ReadResponseStringAsync(request); XElement rootElement = XElement.Parse(result); XElement versionElement = rootElement.Element("version"); XElement downloadElement = rootElement.Element("download"); XElement executeElement = rootElement.Element("execute"); XElement filesElement = rootElement.Element("files"); info.Version = Version.Parse(versionElement.Value); info.Link = downloadElement.Value; info.Execute = executeElement.Value; info.Files = filesElement.Value; return(info); }
public MoreInfo RequestMoreInfo() { HttpWebRequest request = Requests.CreateDefaultRequest(m_datasheetSiteLink); string result = Requests.ReadResponseString(request); HtmlDocument document = new HtmlDocument(); document.LoadHtml(result); HtmlNode rootElement = document.DocumentNode; IEnumerable <HtmlNode> matchingNodes = from node in rootElement.Descendants("td") where IsAttributeValueLike(node, "width", "283") && IsAttributeValueLike(node, "height", "367") && IsAttributeValueLike(node, "align", "center") && IsAttributeValueLike(node, "valign", "top") select node; HtmlNode aNode = matchingNodes.ElementAt(0).Element("a"); MoreInfo moreInfo = new MoreInfo(); moreInfo.PdfSite = GetAttributeValueOrEmpty(aNode, "href"); matchingNodes = from node in rootElement.Descendants("td") where IsAttributeValueLike(node, "height", "40") && IsAttributeValueLike(node, "class", "gray_title") && IsAttributeValueLike(node, "width", "88") select node; HtmlNode sizeNode = matchingNodes.ElementAt(2).ParentNode; moreInfo.Size = sizeNode.Elements("td").ElementAt(1).Element("font").InnerText; HtmlNode pagesNode = matchingNodes.ElementAt(3).ParentNode; moreInfo.Pages = pagesNode.Elements("td").ElementAt(1).Element("font").InnerText; HtmlNode manufacturerSiteNode = matchingNodes.ElementAt(5).ParentNode; moreInfo.ManufacturerSite = manufacturerSiteNode.Elements("td").ElementAt(1).InnerText; return(moreInfo); }
public Stream GetDatasheetStream(string pdfSite) { Uri pdfUri = new Uri(pdfSite); string pdfHost = pdfUri.GetLeftPart(UriPartial.Scheme) + pdfUri.Host; CookieContainer cookies = new CookieContainer(); HttpWebRequest request = Requests.CreateDefaultRequest(pdfSite); request.Referer = m_datasheetSiteLink; request.CookieContainer = cookies; string result = Requests.ReadResponseString(request); HtmlDocument document = new HtmlDocument(); document.LoadHtml(result); HtmlNode rootElement = document.DocumentNode; IEnumerable <HtmlNode> matchingNodes = from node in rootElement.Descendants("iframe") where IsAttributeValueLike(node, "height", "810") && IsAttributeValueLike(node, "name", "333") && IsAttributeValueLike(node, "width", "100%") select node; string pdfPath = GetAttributeValueOrEmpty(matchingNodes.ElementAt(0), "src"); string pdfDirect = pdfHost + pdfPath; cookies.Add(new Uri(pdfDirect), cookies.GetCookies(new Uri(pdfSite))); request = Requests.CreateDefaultRequest(pdfDirect); request.Referer = pdfSite; request.CookieContainer = cookies; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return(response.GetResponseStream()); }
public async void LoadImage() { string file = ImageFileName; if (string.IsNullOrWhiteSpace(file)) { return; } string imagePath = $"{Global.AppDataPath}{Path.DirectorySeparatorChar}{Global.ImagesCacheDirectory}{Path.DirectorySeparatorChar}{file}"; if (!Global.CachedImages.ContainsKey(file)) { Global.CachedImages.Add(file, BitmapImageLoadingInfo.CreateDefault()); } BitmapImageLoadingInfo info = Global.CachedImages[file]; if (info.Loaded) { this.Image = info.Image; } if (info.Loading) { await Task.Run(async() => { // ugly hack to test wheter image is loaded, // assigment of Image.Source before BitmapImage has been loaded causes NullReferenceException in BitmapImage.EndInit() while (info.Loading) { await Task.Delay(100); } }); this.Image = info.Image; } if (info.Loaded || info.Loading) { return; } info.Loading = true; if (File.Exists(imagePath)) { byte[] imageData = File.ReadAllBytes(imagePath); MemoryStream stream = new MemoryStream(imageData); info.Image.BeginInit(); info.Image.CacheOption = BitmapCacheOption.OnLoad; info.Image.StreamSource = stream; info.Image.EndInit(); this.Image = info.Image; info.Loaded = true; } else { MemoryStream memory = new MemoryStream(); bool success = await Task.Run <bool>(() => { Stream stream = null; if (IsContextValid && !m_context.OnlyContext) { stream = m_context.GetManufacturerImageStream(); } else if (!Custom && !string.IsNullOrWhiteSpace(ManufacturerImageLink)) { try { HttpWebRequest request = Requests.CreateDefaultRequest(ManufacturerImageLink); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); stream = response.GetResponseStream(); } catch { stream = null; } } if (stream != null) { FileStream fileStream = new FileStream(imagePath, FileMode.Create); try { byte[] buffer = new byte[1024]; int len; while ((len = stream.Read(buffer, 0, buffer.Length)) > 0) { memory.Write(buffer, 0, len); fileStream.Write(buffer, 0, len); } } finally { stream.Close(); fileStream.Close(); } return(true); } return(false); }); if (success) { memory.Seek(0, SeekOrigin.Begin); info.Image.BeginInit(); info.Image.CacheOption = BitmapCacheOption.OnLoad; info.Image.StreamSource = memory; info.Image.EndInit(); this.Image = info.Image; info.Loaded = true; } } info.Loading = false; }
private async void DoUpdate(string link) { string zipFilePath = $"{Global.AppDataPath}{Path.DirectorySeparatorChar}{Global.UpdateFile}"; string extractPath = $"{Global.AppDataPath}{Path.DirectorySeparatorChar}{Global.UpdateExtractDirectory}"; try { HttpWebRequest request = Requests.CreateDefaultRequest(link); using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync()) { using (Stream stream = response.GetResponseStream()) { Text = Global.GetStringResource("StringDownloadingUpdate"); using (FileStream file = new FileStream(zipFilePath, FileMode.Create)) { int len; byte[] buffer = new byte[4096]; while ((len = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) { await file.WriteAsync(buffer, 0, len); Progress = (int)((decimal)(file.Position - 1) / (decimal)response.ContentLength * 50M); Text = $"{Global.GetStringResource("StringDownloadingUpdate")}{Environment.NewLine}{(file.Position - 1) / 1024}/{response.ContentLength / 1024} KB"; } } } } } catch { Global.Dialogs.Close(this); return; } m_downloadSuccessful = true; Text = Global.GetStringResource("StringExtractingUpdate"); try { if (Directory.Exists(extractPath)) { Directory.Delete(extractPath, true); } await Task.Run(() => { long totalSize = 0; long transferred = 0; long oldTransferred = 0; ZipEntry oldEntry = null; using (ZipFile zip = new ZipFile(zipFilePath)) { foreach (ZipEntry item in zip.Entries) { totalSize += item.UncompressedSize; } zip.ExtractProgress += (s, e) => { if (e.CurrentEntry != oldEntry) { oldEntry = e.CurrentEntry; oldTransferred = 0; } if (e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten) { transferred += e.BytesTransferred - oldTransferred; oldTransferred = e.BytesTransferred; } InvokeWindow.Invoke(() => { Text = $"{Global.GetStringResource("StringExtractingUpdate")}{Environment.NewLine}{transferred / 1024}/{totalSize / 1024} KB"; Progress = 50 + (int)((decimal)transferred / (decimal)totalSize * 50M); }); }; zip.ExtractAll(extractPath, ExtractExistingFileAction.OverwriteSilently); } }); } catch { Global.Dialogs.Close(this); return; } m_extractSuccessful = true; Global.Dialogs.Close(this); }