Exemple #1
0
        public async Task <object> Post(PageInfoRequest request)
        {
            string address = request.Address;

            PageHtmlDownloader.Result result;
            string source;

            PageHtmlDownloader downloader = new PageHtmlDownloader();

            try
            {
                result = await downloader.Download(address);

                if (result == null)
                {
                    return(new { Error = "PageNotAvailable" });
                }
                else
                {
                    source = result.Source;
                }
            }
            catch (UriFormatException)
            {
                return(new { Error = "InvalidAddress" });
            }

            string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;

            FaviconLoader loader = new FaviconLoader();

            FaviconLoader.Result favicon = await loader.Load(result.ResultUri, source);

            return(new { Error = "", Title = title, FaviconUrl = favicon?.FaviconUrl, FaviconData = favicon?.Data, FaviconMime = favicon?.MimeType, ResultUrl = result.ResultUri.ToString().TrimEnd('/') });
        }
        private static void TestIcon(FaviconLoader loader, string url)
        {
            Task <string> task = GetHtml(url);
            string        html = GetHtml(url).Result;

            Assert.IsNotNull(html);

            FaviconLoader.Result result = loader.Load(new Uri(url), html).Result;

            if (result != null)
            {
                Assert.IsTrue(result.Data != null || result.FaviconUrl == null);

                Size size = new FaviconSizeDetector().DetectSize(result.Data);

                Console.WriteLine(result.FaviconUrl + " " + result.MimeType + " " + size);
            }
            else
            {
                Console.WriteLine(url + ": NO FAVICON");
            }
        }