public async Task<byte[]> GetImageData (string resourceUrl)
		{
			byte[] data = null;

			using (var c = new GzipWebClient ())
				data = await c.DownloadDataTaskAsync(resourceUrl);

			return data;
		}
		public static async Task<HtmlDocument> GetHtmlDocGzip(string url)
		{
			using(var wc = new GzipWebClient())
			{
				wc.Encoding = Encoding.UTF8;
				// add an user-agent to stop some 403's
				wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0");

				var websiteContent = await wc.DownloadStringTaskAsync(new Uri(url));
				using(var reader = new StringReader(websiteContent))
				{
					var doc = new HtmlDocument();
					doc.Load(reader);
					return doc;
				}
			}
		}
Beispiel #3
0
        private void FinishScenario()
        {
            var measurement = new Measurement(DateTime.UtcNow.Ticks, _stopwatch.ElapsedMilliseconds);

            switch (_scenario)
            {
            case -1:
                // Don't record the first measurement; it's always very high
                _scenario++;
                break;

            case 0:
                BeginGetResponse.Add(measurement);
                _scenario++;
                break;

            case 1:
                BeginGetCompressedResponse.Add(measurement);
                _scenario++;
                break;

            case 2:
                WebClient.Add(measurement);
                _scenario++;
                break;

            case 3:
                GzipWebClient.Add(measurement);
#if SHARPGIS
                _scenario++;
                break;

            case 4:
                SharpGIS.Add(measurement);
#endif
                _scenario = 0;
                break;

            default:
                Debug.Assert(false);
                break;
            }

            StartNextScenario();
        }
Beispiel #4
0
        public static async Task <HtmlDocument> GetHtmlDocGzip(string url)
        {
            using (var wc = new GzipWebClient())
            {
                wc.Encoding = Encoding.UTF8;
                // add an user-agent to stop some 403's
                wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0");

                var websiteContent = await wc.DownloadStringTaskAsync(new Uri(url));

                using (var reader = new StringReader(websiteContent))
                {
                    var doc = new HtmlDocument();
                    doc.Load(reader);
                    return(doc);
                }
            }
        }
Beispiel #5
0
        private void StartNextScenario()
        {
            if (stop)
            {
                return;
            }

            _stopwatch.Reset();
            _stopwatch.Start();
            HttpWebRequest request = null;
            WebClient      client  = null;

            switch (_scenario)
            {
            case -1:
            case 0:
                request = WebRequest.CreateHttp(_uri);
                request.BeginGetResponse(ContinueScenario, request);
                break;

            case 1:
                request = WebRequest.CreateHttp(_uri);
                request.BeginGetCompressedResponse(ContinueScenario, request);
                break;

            case 2:
                client = new WebClient();
                break;

            case 3:
                client = new GzipWebClient();
                break;

            default:
                Debug.Assert(false);
                break;
            }

            if (null != client)
            {
                client.OpenReadCompleted += ContinueScenario;
                client.OpenReadAsync(_uri);
            }
        }
Beispiel #6
0
        private void StartNextScenario()
        {
            _stopwatch.Reset();
            _stopwatch.Start();
            HttpWebRequest request = null;
            WebClient      client  = null;

            switch (_scenario)
            {
            case -1:
            case 0:
                request = WebRequest.CreateHttp(_uri);
                request.BeginGetResponse(ContinueScenario, request);
                break;

            case 1:
                request = WebRequest.CreateHttp(_uri);
                request.BeginGetCompressedResponse(ContinueScenario, request);
                break;

            case 2:
                client = new WebClient();
                break;

            case 3:
                client = new GzipWebClient();
                break;

#if SHARPGIS
            case 4:
                client = new SharpGIS.GZipWebClient();
                break;
#endif
            default:
                Debug.Assert(false);
                break;
            }
            if (null != client)
            {
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(ContinueScenario);
                client.OpenReadAsync(_uri);
            }
        }
Beispiel #7
0
        /// <summary>
        /// I really should make this async. Load a remote page by URL and attempt to get details about it.
        /// </summary>
        public async Task <ServiceModels.RemotePageDetails> GetRemotePageDetails(string remoteUrl)
        {
            var uri = new Uri(remoteUrl);
            var remoteUrlAuthority = uri.GetLeftPart(UriPartial.Authority);
            var domain             = uri.Host.Replace("/www.", "/").ToLower();

            var faviconPath        = $"{remoteUrlAuthority}/favicon.ico";
            var faviconStoragePath = await CacheFavicon(domain, uri.GetLeftPart(UriPartial.Path), faviconPath);

            var returnResult = new ServiceModels.RemotePageDetails {
                Title = remoteUrl,
            };

            // TODO - This is boring. Pull the topic titles. Maybe finally do OG tags?
            if (domain == "warpstorm.com")
            {
                returnResult.Title = "Warpstorm";
                return(returnResult);
            }

            var siteWithoutHash = remoteUrl.Split('#')[0];

            var document = new HtmlDocument();

            try {
                var client = new GzipWebClient();
                var data   = client.DownloadString(siteWithoutHash);

                if (string.IsNullOrEmpty(data))
                {
                    return(returnResult);
                }

                document.LoadHtml(data);
            }
            catch (UriFormatException) { }
            catch (AggregateException) { }
            catch (ArgumentException) { }
            catch (WebException) { }

            var titleTag = document.DocumentNode.SelectSingleNode(@"//title");

            if (titleTag != null && !string.IsNullOrEmpty(titleTag.InnerText.Trim()))
            {
                returnResult.Title = titleTag.InnerText.Trim();
            }

            if (string.IsNullOrEmpty(faviconStoragePath))
            {
                var element = document.DocumentNode.SelectSingleNode(@"//link[@rel='shortcut icon']");

                if (element != null)
                {
                    faviconPath        = element.Attributes["href"].Value.Trim();
                    faviconStoragePath = await CacheFavicon(domain, uri.GetLeftPart(UriPartial.Path), faviconPath);
                }
            }

            if (string.IsNullOrEmpty(faviconStoragePath))
            {
                var element = document.DocumentNode.SelectSingleNode(@"//link[@rel='icon']");

                if (element != null)
                {
                    faviconPath        = element.Attributes["href"].Value.Trim();
                    faviconStoragePath = await CacheFavicon(domain, uri.GetLeftPart(UriPartial.Path), faviconPath);
                }
            }

            returnResult.Favicon = faviconStoragePath;

            // try to find the opengraph title
            var ogTitle       = document.DocumentNode.SelectSingleNode(@"//meta[@property='og:title']");
            var ogSiteName    = document.DocumentNode.SelectSingleNode(@"//meta[@property='og:site_name']");
            var ogImage       = document.DocumentNode.SelectSingleNode(@"//meta[@property='og:image']");
            var ogDescription = document.DocumentNode.SelectSingleNode(@"//meta[@property='og:description']");

            if (ogTitle != null && ogTitle.Attributes["content"] != null && !string.IsNullOrEmpty(ogTitle.Attributes["content"].Value.Trim()))
            {
                returnResult.Title = ogTitle.Attributes["content"].Value.Trim();

                if (ogDescription != null && ogDescription.Attributes["content"] != null && !string.IsNullOrEmpty(ogDescription.Attributes["content"].Value.Trim()))
                {
                    returnResult.Card += "<blockquote class='card pointer hover-highlight' clickable-link-parent>";

                    if (ogImage != null && ogImage.Attributes["content"] != null && !string.IsNullOrEmpty(ogImage.Attributes["content"].Value.Trim()))
                    {
                        var imagePath = ogImage.Attributes["content"].Value.Trim();

                        if (imagePath.StartsWith("/"))
                        {
                            imagePath = $"{remoteUrlAuthority}{imagePath}";
                        }

                        returnResult.Card += $"<div class='card-image'><img src='{imagePath}' /></div>";
                    }

                    returnResult.Card += "<div>";
                    returnResult.Card += "<p class='card-title'><a target='_blank' href='" + remoteUrl + "'>" + returnResult.Title + "</a></p>";

                    var decodedDescription = WebUtility.HtmlDecode(ogDescription.Attributes["content"].Value.Trim());

                    returnResult.Card += "<p class='card-description'>" + decodedDescription + "</p>";

                    if (ogSiteName != null && ogSiteName.Attributes["content"] != null && !string.IsNullOrEmpty(ogSiteName.Attributes["content"].Value.Trim()))
                    {
                        returnResult.Card += "<p class='card-link'><a target='_blank' href='" + remoteUrl + "'>[" + ogSiteName.Attributes["content"].Value.Trim() + "]</a></p>";
                    }
                    else
                    {
                        returnResult.Card += "<p class='card-link'><a target='_blank' href='" + remoteUrl + "'>[Direct Link]</a></p>";
                    }

                    returnResult.Card += "</div><br class='clear' /></blockquote>";
                }
            }

            if (returnResult.Title.Contains(" - "))
            {
                // One-off action. If there are more, replace this with a switch statement.
                if (domain == "bulbapedia.bulbagarden.net")
                {
                    returnResult.Title = returnResult.Title.Split(" - ")[0];
                }
            }

            return(returnResult);
        }
		void BeginDownloadingImage (RSSFeedItem app, NSIndexPath path)
		{
			// Queue the image to be downloaded. This task will execute
			// as soon as the existing ones have finished.
			byte[] data = null;
			DownloadTask = DownloadTask.ContinueWith (prevTask => {
				try {
					UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
					using (var c = new GzipWebClient ())
						data = c.DownloadData (app.Image);
				} finally {
					UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
				}
			});

			// When the download task is finished, queue another task to update the UI.
			// Note that this task will run only if the download is successful and it
			// uses the CurrentSyncronisationContext, which on MonoTouch causes the task
			// to be run on the main UI thread. This allows us to safely access the UI.
			DownloadTask = DownloadTask.ContinueWith (t => {
				// Load the image from the byte array.
				app.TheImage = UIImage.LoadFromData (NSData.FromArray (data));

				// Retrieve the cell which corresponds to the current App. If the cell is null, it means the user
				// has already scrolled that app off-screen.
				var cell = TableView.VisibleCells.Where (c => c.Tag == viewModel.FeedItems.IndexOf (app)).FirstOrDefault ();
				if (cell != null)
					cell.ImageView.Image = app.TheImage;
			}, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext ());
		}
			async Task<byte[]> GetImageData(String imageUrl)
			{
				byte[] data = null;
				try {
					UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
					using (var c = new GzipWebClient ())
						data = await c.DownloadDataTaskAsync (imageUrl);
				} 
				catch(Exception){

				}
				finally {
					UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
				}

				return data;
			}