Пример #1
0
        public async Task <WebInfo> CreateWebInfo(string url)
        {
            if (!WebInfos.ContainsKey(url))
            {
                Console.WriteLine("Going to the web to get size...");
                WebInfo webInfo = await WriteWebRequestSizeAsync(url);

                WebInfos.Add(webInfo.Url, webInfo);
            }
            else
            {
                WebInfo currWebInfo = WebInfos[url];
                Console.WriteLine("Getting size from cache...");
                Console.WriteLine($"Url: {currWebInfo.Url} Size: {currWebInfo.Size} Timestamp: {currWebInfo.Timestamp}");
            }

            return(await Task.FromResult(WebInfos[url].Clone()));
        }
Пример #2
0
        private static async Task <WebInfo> WriteWebRequestSizeAsync(string url)
        {
            WebInfo webInfo = new WebInfo();

            webInfo.Url       = url;
            webInfo.Timestamp = DateTime.Now;
            try
            {
                WebRequest webRequest =
                    WebRequest.Create(url);
                WebResponse response =
                    await webRequest.GetResponseAsync();

                using (StreamReader reader =
                           new StreamReader(
                               response.GetResponseStream()))
                {
                    string text =
                        await reader.ReadToEndAsync();

                    Console.WriteLine(
                        FormatBytes(text.Length));
                    webInfo.Size = FormatBytes(text.Length);
                }
            }
            catch (WebException)
            {
                // ...
            }
            catch (IOException)
            {
                // ...
            }
            catch (NotSupportedException)
            {
                // ...
            }
            return(await Task.FromResult(webInfo));
        }