OpenReadTaskAsync() public method

public OpenReadTaskAsync ( System address ) : System.Threading.Tasks.Task
address System
return System.Threading.Tasks.Task
		async Task<int> GetBytes(string url, CancellationToken cancelToken, IProgress<DownloadBytesProgress> progressIndicator)
		{
			int receivedBytes = 0;
			int totalBytes = -1;
			WebClient client = new WebClient();

			using (var stream = await client.OpenReadTaskAsync(url))
			{
				byte[] buffer = new byte[4096];
				Int32.TryParse(client.ResponseHeaders[HttpResponseHeader.ContentLength], out totalBytes);

				for (;;)
				{
					int len = await stream.ReadAsync(buffer, 0, buffer.Length);
					if (len == 0)
					{
						await Task.Yield();
						break;
					}

					receivedBytes += len;
					cancelToken.ThrowIfCancellationRequested();

					if (progressIndicator != null)
					{
						DownloadBytesProgress args = new DownloadBytesProgress(url, receivedBytes, totalBytes);
						progressIndicator.Report(args);
					}
				}
			}
			return receivedBytes;
		}
Exemplo n.º 2
0
public static async Task<int> CreateDownloadTask(string urlToDownload, IProgress<DownloadBytesProgress> progessReporter)
{
	int receivedBytes = 0;
	int totalBytes = 0;
	WebClient client = new WebClient();

	using (var stream = await client.OpenReadTaskAsync(urlToDownload))
	{
		byte[] buffer = new byte[BufferSize];
		totalBytes = Int32.Parse(client.ResponseHeaders[HttpResponseHeader.ContentLength]);

		for (;;)
		{
			int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
			if (bytesRead == 0)
			{
				await Task.Yield();
				break;
			}

			receivedBytes += bytesRead;
			if (progessReporter != null)
			{
				DownloadBytesProgress args = new DownloadBytesProgress(urlToDownload, receivedBytes, totalBytes);
				progessReporter.Report(args);
			}
		}
	}
	return receivedBytes;
}
Exemplo n.º 3
0
        public async Task UpdateListFromRemote()
        {
            var s = StorageHelper.ReadFromLocalCache(ConfigFileName, CacheDays);
            if (s != null)
            {
                s.Dispose();
                return;
            }

            var client = new WebClient();
            try
            {
                s = await client.OpenReadTaskAsync(RemoteFilePath);
                Debug.WriteLine("Update server list from remote");
                UpdateList(s);

                RefreshList();

                using (s)
                {
                    using (var reader = new StreamReader(s))
                    {
                        s.Seek(0, SeekOrigin.Begin);
                        StorageHelper.WriteToLocalCache(ConfigFileName, reader.ReadToEnd());
                    }
                }
            }
            catch (Exception) { }
        }
Exemplo n.º 4
0
        private Task<IEnumerable<string>> GetImageList(string artistName, CancellationToken token)
        {
            var replacedSpaces = artistName.Replace(" ", "_");
            var siteUri = new Uri("http://htbackdrops.com/api/" + ApiKey + "/searchXML?keywords=" + replacedSpaces + "&limit=7");
            var client = new WebClient();

            return client
                .OpenReadTaskAsync(siteUri)
                .ContinueWith(readTask =>
                {
                    if (readTask.IsFaulted)
                    {
                        Trace.WriteLine(readTask.Exception);
                        return new string[0];
                    }

                    var doc = new XmlDocument();
                    doc.Load(readTask.Result);

                    XmlNodeList nodelist = doc.SelectNodes("/search/images/image/id");

                    if (nodelist == null || nodelist.Count == 0)
                    {
                        return new string[0];
                    }

                    return nodelist
                        .OfType<XmlNode>()
                        .Select(node => node.InnerText)
                        .Select(imageId => "http://htbackdrops.com/api/" + ApiKey + "/download/" + imageId + "/intermediate");
                }, token);
        }
Exemplo n.º 5
0
 public Task<Stream> GetAsStreamAsync()
 {
     using (var webClient = new WebClient())
     {
         return webClient.OpenReadTaskAsync(_url);
     }
 }
Exemplo n.º 6
0
 static async Task<string> ReadStreamAsync(string url)
 {
     using (var client = new WebClient())
     using (var reader = new StreamReader(await client.OpenReadTaskAsync(url)))
     {
         return await reader.ReadToEndAsync();
     }
 }
Exemplo n.º 7
0
        public void RSPEC_WebClient(string address, Uri uriAddress, byte[] data,
                                    NameValueCollection values)
        {
            System.Net.WebClient webclient = new System.Net.WebClient();

            // All of the following are Questionable although there may be false positives if the URI scheme is "ftp" or "file"
            //webclient.Download * (...); // Any method starting with "Download"
            webclient.DownloadData(address);                            // Noncompliant
            webclient.DownloadDataAsync(uriAddress, new object());      // Noncompliant
            webclient.DownloadDataTaskAsync(uriAddress);                // Noncompliant
            webclient.DownloadFile(address, "filename");                // Noncompliant
            webclient.DownloadFileAsync(uriAddress, "filename");        // Noncompliant
            webclient.DownloadFileTaskAsync(address, "filename");       // Noncompliant
            webclient.DownloadString(uriAddress);                       // Noncompliant
            webclient.DownloadStringAsync(uriAddress, new object());    // Noncompliant
            webclient.DownloadStringTaskAsync(address);                 // Noncompliant

            // Should not raise for events
            webclient.DownloadDataCompleted   += Webclient_DownloadDataCompleted;
            webclient.DownloadFileCompleted   += Webclient_DownloadFileCompleted;
            webclient.DownloadProgressChanged -= Webclient_DownloadProgressChanged;
            webclient.DownloadStringCompleted -= Webclient_DownloadStringCompleted;


            //webclient.Open * (...); // Any method starting with "Open"
            webclient.OpenRead(address);
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^   {{Make sure that this http request is sent safely.}}
            webclient.OpenReadAsync(uriAddress, new object());              // Noncompliant
            webclient.OpenReadTaskAsync(address);                           // Noncompliant
            webclient.OpenWrite(address);                                   // Noncompliant
            webclient.OpenWriteAsync(uriAddress, "STOR", new object());     // Noncompliant
            webclient.OpenWriteTaskAsync(address, "POST");                  // Noncompliant

            webclient.OpenReadCompleted  += Webclient_OpenReadCompleted;
            webclient.OpenWriteCompleted += Webclient_OpenWriteCompleted;

            //webclient.Upload * (...); // Any method starting with "Upload"
            webclient.UploadData(address, data);                           // Noncompliant
            webclient.UploadDataAsync(uriAddress, "STOR", data);           // Noncompliant
            webclient.UploadDataTaskAsync(address, "POST", data);          // Noncompliant
            webclient.UploadFile(address, "filename");                     // Noncompliant
            webclient.UploadFileAsync(uriAddress, "filename");             // Noncompliant
            webclient.UploadFileTaskAsync(uriAddress, "POST", "filename"); // Noncompliant
            webclient.UploadString(uriAddress, "data");                    // Noncompliant
            webclient.UploadStringAsync(uriAddress, "data");               // Noncompliant
            webclient.UploadStringTaskAsync(uriAddress, "data");           // Noncompliant
            webclient.UploadValues(address, values);                       // Noncompliant
            webclient.UploadValuesAsync(uriAddress, values);               // Noncompliant
            webclient.UploadValuesTaskAsync(address, "POST", values);
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

            // Should not raise for events
            webclient.UploadDataCompleted   += Webclient_UploadDataCompleted;
            webclient.UploadFileCompleted   += Webclient_UploadFileCompleted;
            webclient.UploadProgressChanged -= Webclient_UploadProgressChanged;
            webclient.UploadStringCompleted -= Webclient_UploadStringCompleted;
            webclient.UploadValuesCompleted -= Webclient_UploadValuesCompleted;
        }
Exemplo n.º 8
0
 async public Task <Image> GetImage(string fromUrl)
 {
     using (System.Net.WebClient webClient = new System.Net.WebClient())
     {
         using (Stream stream = await webClient.OpenReadTaskAsync(fromUrl))
         {
             return(System.Drawing.Image.FromStream(stream));
         }
     }
 }
        /// <summary>
        /// Load data from flickr
        /// </summary>
        /// <param name="tags"></param>
        /// <returns></returns>
        private static async Task<XmlNodeList> LoadDataAsync(string tags)
        {
            var webClient = new WebClient();
            var stream = await
                webClient.OpenReadTaskAsync(
                    new Uri($"http://api.flickr.com/services/feeds/photos_public.gne?tags={tags}"));

            var xdoc = new XmlDocument();//xml doc used for xml parsing
            xdoc.Load(stream);
            var xNodelst = xdoc.GetElementsByTagName("entry");
            return xNodelst;
        }
Exemplo n.º 10
0
 public async Task<BitmapSource> GetPhotoAsync(string uri)
 {
     WebClient client = new WebClient();
     Stream stream = await client.OpenReadTaskAsync(uri);
     try
     {
         return PictureDecoder.DecodeJpeg(stream);
     }
     catch (COMException)
     {
         return null;
     }
 }
        /// <summary>
        /// Downloads an xml file from AniDB which contains all of the titles for every anime, and their IDs,
        /// and saves it to disk.
        /// </summary>
        /// <param name="titlesFile">The destination file name.</param>
        private async Task DownloadTitles(string titlesFile)
        {
            _logger.Debug("Downloading new AniDB titles file.");

            var client = new WebClient();

            await AniDbSeriesProvider.RequestLimiter.Tick();

            using (var stream = await client.OpenReadTaskAsync(TitlesUrl))
            using (var unzipped = new GZipStream(stream, CompressionMode.Decompress))
            using (var writer = File.Open(titlesFile, FileMode.Create, FileAccess.Write))
            {
                await unzipped.CopyToAsync(writer).ConfigureAwait(false);
            }
        }
Exemplo n.º 12
0
 /// <summary>
 /// Check if the internet is available. It connects to google.com
 /// </summary>
 /// <returns>If the connection to google was successful</returns>
 public async static Task<bool> CheckForInternetConnection()
 {
     try
     {
         using (var client = new WebClient { Proxy = null })
         using (await client.OpenReadTaskAsync("http://www.google.com"))
         {
             return true;
         }
     }
     catch
     {
         return false;
     }
 }
Exemplo n.º 13
0
 private async Task<SyndicationFeed> DownloadFeed(string url)
 {
     try
     {
         using (WebClient client = new WebClient())
         {
             var stream = await client.OpenReadTaskAsync(url);
             return SyndicationFeed.Load(XmlReader.Create(stream));
         }
     }
     catch (Exception)
     {
         return new SyndicationFeed();
     }
 }
Exemplo n.º 14
0
        /// <summary>
        /// Loads an image from an Url asynchronously
        /// </summary>
        /// <param name="location">The location of the image</param>
        /// <returns>A BitmapImage, that can be set as a source</returns>
        public static async Task<ExtendedImage> LoadImageFromUrlAsync(Uri location)
        {
            WebClient client = new WebClient();
            ExtendedImage image = new ExtendedImage();
            Stream source = await client.OpenReadTaskAsync(location);

            if (location.ToString().EndsWith("gif", StringComparison.InvariantCultureIgnoreCase))
            {
                image.SetSource(source);

                TaskCompletionSource<ExtendedImage> imageLoaded = new TaskCompletionSource<ExtendedImage>();

                EventHandler loadingCompleteHandler = new EventHandler((sender, e) =>
                {
                    imageLoaded.SetResult(image);
                });

                EventHandler<UnhandledExceptionEventArgs> loadingFailedHandler = new EventHandler<UnhandledExceptionEventArgs>((sender, e) =>
                {
                    imageLoaded.SetResult(image);
#if DEBUG
                    if (System.Diagnostics.Debugger.IsAttached)
                        System.Diagnostics.Debugger.Break();
#endif
                });



                image.LoadingCompleted += loadingCompleteHandler;
                image.LoadingFailed += loadingFailedHandler;

                image = await imageLoaded.Task;

                //Remove handlers, otherwise the object might be kept in the memory
                image.LoadingCompleted -= loadingCompleteHandler;
                image.LoadingFailed -= loadingFailedHandler;
            }
            else
            {
                BitmapImage bmp = new BitmapImage();
                bmp.SetSource(source);
                WriteableBitmap writeable = new WriteableBitmap(bmp);
                image = ImageExtensions.ToImage(writeable);
            }

            source.Close();
            return image;
        }
Exemplo n.º 15
0
 private async Task<SyndicationFeed> DownloadFeed(string url)
 {
     try
     {
         using (WebClient client = new WebClient())
         {
             var stream = await client.OpenReadTaskAsync(url);
             return SyndicationFeed.Load(XmlReader.Create(stream));
         }
     }
     catch (Exception ex)
     {
         Trace.Warn("Feed Collector", "Couldn't download: " + url, ex);
         return new SyndicationFeed();
     }
 }
Exemplo n.º 16
0
 public async Task<bool> HasConnectionAsync(string uri)
 {
     try
     {
         Uri theUri;
         Uri.TryCreate(uri, UriKind.RelativeOrAbsolute, out theUri);
         using (var client = new WebClient())
         {
             await client.OpenReadTaskAsync(uri);
         }
     }
     catch
     {
         return false;
     }
     return true;
 }
Exemplo n.º 17
0
        public async Task <TApiQueryResult> Query <TApiQueryResult>(IApiQuery <TApiQueryResult> query)
        {
            using (var webClient = new System.Net.WebClient())
            {
                webClient.QueryString = query.Parameters.Aggregate(new NameValueCollection(),
                                                                   (seed, current) => {
                    seed.Add(current.Key, current.Value);
                    return(seed);
                });
                using (var stream = await webClient.OpenReadTaskAsync(QUERY_URL))
                {
                    var result = _deserializer.Deserialize <TApiQueryResult>(stream);

                    return(result);
                }
            }
        }
Exemplo n.º 18
0
        private async Task<SyndicationFeed> LoadFeed()
        {
            SyndicationFeed feed;

            using (WebClient wc = new WebClient())
            using (Stream stream = await wc.OpenReadTaskAsync(new Uri("http://businessoftech.com/podcast/feed/1")))
            using (XmlReader reader = XmlReader.Create(stream, new XmlReaderSettings
            {
                CheckCharacters = true,
            }))
            {
                feed = SyndicationFeed.Load(reader);
            }

            feed.AttributeExtensions.Add(new XmlQualifiedName("itunes", "http://www.w3.org/2000/xmlns/"), Constants.ItunesNS.NamespaceName);

            return feed;
        }
Exemplo n.º 19
0
	static async Task GetWebContentAsync()
	{
		WebClient myClient = new WebClient();
		Stream response = await myClient.OpenReadTaskAsync("http://www.contoso.com/index.htm");
	
		using (var reader = new StreamReader(response))
		{
			string line;
			while((line = await reader.ReadLineAsync()) != null)
			{
				Console.WriteLine();
				Console.WriteLine(line);
			}

			reader.Close();
		}

		response.Close();
	}
Exemplo n.º 20
0
        public virtual async Task<int> CreateDownloadTask(
            string url,
            IProgress<DownloadBytesProgress> progressReporter
        )
        {
            var receivedBytes = 0;
            var client = new WebClient ();

            OnStartWait ();
            using (var storage = OpenStorage (url))
            using (var stream = await client.OpenReadTaskAsync (url))
            {
                _throttle.WaitOne ();
                OnStopWait ();

                var buffer = new byte[BUFFER_SIZE];
                var totalBytes = Int32.Parse (client.ResponseHeaders [HttpResponseHeader.ContentLength]);

                while (true)
                {
                    var bytesRead = await stream.ReadAsync (buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    {
                        _throttle.Release ();
                        break;
                    }

                    Decrypt (buffer, bytesRead);

                    storage.Write (buffer, 0, bytesRead);

                    receivedBytes += bytesRead;
                    if (progressReporter != null)
                    {
                        var args = new DownloadBytesProgress (url, receivedBytes, totalBytes);
                        progressReporter.Report (args);
                    }
                }
            }

            return receivedBytes;
        }
        public async Task<byte[]> DownloadAsync(string url, IDictionary<string, string> headers, string method)
        {
            var client = new WebClient();
            if (headers != null)
            {
                foreach (var header in headers)
                    client.Headers[header.Key] = header.Value;
            }
            var downloadStream = await client.OpenReadTaskAsync(url);
            byte[] buffer = new byte[1024];
            using (MemoryStream stream = new MemoryStream())
            {
                while (downloadStream.Read(buffer, 0, buffer.Length) > 0)
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
                await stream.FlushAsync();
                return stream.ToArray();
            }

        }
        private async Task<SyndicationFeed> DownloadFeed(string url)
        {
            if (string.IsNullOrWhiteSpace(url))
                return new SyndicationFeed();

            try
            {
                using (WebClient client = new WebClient())
                {
                    var stream = await client.OpenReadTaskAsync(url);
                    var settings = new XmlReaderSettings();
                    settings.DtdProcessing = DtdProcessing.Parse;
                    return SyndicationFeed.Load(XmlReader.Create(stream, settings));
                }
            }
            catch (Exception exc)
            {
                this.Logger.AddMessage("DownloadFeed", $"exception {exc.Message} while downloading Feed", url, MessageType.Error);
                return new SyndicationFeed();
            }
        }
Exemplo n.º 23
0
        public static async Task <Stream> DownloadFileAsStreamAsync(string downloadUrl, CancellationToken token)
        {
            using (System.Net.WebClient webClient = new System.Net.WebClient())
            {
                int receivedBytes = 0;

                Stream stream = await webClient.OpenReadTaskAsync(downloadUrl);

                MemoryStream ms         = new MemoryStream();
                var          buffer     = new byte[4096];
                int          read       = 0;
                var          totalBytes = Int32.Parse(webClient.ResponseHeaders[HttpResponseHeader.ContentLength]);

                while ((read = await stream.ReadAsync(buffer, 0, buffer.Length, token)) > 0)
                {
                    ms.Write(buffer, 0, read);
                    receivedBytes += read;
                }
                stream.Close();
                return(ms);
            }
        }
Exemplo n.º 24
0
        private async Task<BitmapImage> Download(string imageUrl, string imageName, string imageSaveName)
        {
            Stream imageStream = null;

            var client = new WebClient();
            try
            {
                var imageUri = new Uri(string.Format(imageUrl, imageName));
                imageStream = await client.OpenReadTaskAsync(imageUri);
            }
            catch (Exception ex)
            {
                // Let it fail if not something catastrophic
                if (!(ex is WebException))
                    throw;
            }

            if (imageStream == null)
                return null;

            var streamResourceInfo = new StreamResourceInfo(imageStream, null);
            var streamReader = new StreamReader(streamResourceInfo.Stream);
               
            byte[] imageBytes;
            using (var binaryReader = new BinaryReader(streamReader.BaseStream))
                imageBytes = binaryReader.ReadBytes((int) streamReader.BaseStream.Length);

            var image = new BitmapImage();
            string name = imageSaveName ?? imageName;
            using (var stream = _isolatedStorageFile.CreateFile(name))
            {
                stream.Write(imageBytes, 0, imageBytes.Length);
                image.SetSource(stream);
            }

            return image;
        }
Exemplo n.º 25
0
		[Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
		public void DownloadMultiple ()
		{
			WebClient wc = new WebClient ();
			var t1 = wc.OpenReadTaskAsync ("http://www.google.com/");
			Assert.That (t1.Wait (15000));
			Assert.IsTrue (t1.IsCompleted, "#1");

			var t2 = wc.OpenReadTaskAsync ("http://www.mono-project.com/");
			Assert.That (t2.Wait (15000));
			Assert.IsTrue (t2.IsCompleted, "#2");

			var t3 = wc.DownloadStringTaskAsync ("http://www.google.com/");
			Assert.That (t3.Wait (15000));
			Assert.IsTrue (t3.IsCompleted, "#3");
		}
Exemplo n.º 26
0
        private async void DownloadImage(string uri) {
            try {
                var client = new WebClient();
                var bi = new BitmapImage();
                bi.SetSource(await client.OpenReadTaskAsync(uri));
                var wb = new WriteableBitmap(bi);
                using(var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
                    if(!isoStore.FileExists(Number.ToString() + ".jpg")) {
                        using(var isoStoreFs = new IsolatedStorageFileStream(Number.ToString() + ".jpg", System.IO.FileMode.Create, isoStore)) {
                            wb.SaveJpeg(isoStoreFs, wb.PixelWidth, wb.PixelHeight, 0, 100);
                        }
                    } else { }
                }
                OnPropertyChanged("Image");
            } catch(Exception) { }

        }
Exemplo n.º 27
0
        public async Task UpdatePackageInfoAsync(PipPackageView package, CancellationToken cancel) {
            string description = null;
            List<string> versions = null;

            using (var client = new WebClient()) {
                Stream data;
                try {
                    data = await client.OpenReadTaskAsync(new Uri(_index ?? DefaultIndex, package.Name + "/json"));
                } catch (WebException) {
                    // No net access
                    return;
                }

                try {
                    using (var reader = JsonReaderWriterFactory.CreateJsonReader(data, new XmlDictionaryReaderQuotas())) {
                        var doc = XDocument.Load(reader);

                        // TODO: Get package URL
                        //url = (string)doc.Document
                        //    .Elements("root")
                        //    .Elements("info")
                        //    .Elements("package_url")
                        //    .FirstOrDefault();

                        description = (string)doc.Document
                            .Elements("root")
                            .Elements("info")
                            .Elements("description")
                            .FirstOrDefault();
                        
                        versions = doc.Document
                            .Elements("root")
                            .Elements("releases")
                            .Elements()
                            .Attributes("item")
                            .Select(a => a.Value)
                            .ToList();
                    }
                } catch (InvalidOperationException) {
                }
            }

            cancel.ThrowIfCancellationRequested();

            bool changed = false;

            await _cacheLock.WaitAsync();
            try {
                PipPackageView inCache;
                if (!_cache.TryGetValue(package.Name, out inCache)) {
                    inCache = _cache[package.Name] = new PipPackageView(this, package.Name, null, null);
                }

                if (!string.IsNullOrEmpty(description)) {
                    var lines = description.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
                    var firstLine = string.Join(
                        " ",
                        lines.TakeWhile(s => !IsSeparatorLine(s)).Select(s => s.Trim())
                    );
                    if (firstLine.Length > 500) {
                        firstLine = firstLine.Substring(0, 497) + "...";
                    }
                    if (firstLine == "UNKNOWN") {
                        firstLine = string.Empty;
                    }

                    inCache.Description = firstLine;
                    package.Description = firstLine;
                    changed = true;
                }

                if (versions != null) {
                    var updateVersion = Pep440Version.TryParseAll(versions)
                        .Where(v => v.IsFinalRelease)
                        .OrderByDescending(v => v)
                        .FirstOrDefault();
                    inCache.UpgradeVersion = updateVersion;
                    package.UpgradeVersion = updateVersion;
                    changed = true;
                }
            } finally {
                _cacheLock.Release();
            }

            if (changed) {
                TriggerWriteCacheToDisk();
            }
        }
        private void RequestRiverBedHomePageAsync(
            IMethodLogger logger, int numberOfRequestsToMake)
        {
            var requestTimes = new ConcurrentBag<long>();
            int htmlLength = 0;

            try
            {
                var tasks =
                    Enumerable.Range(0, numberOfRequestsToMake)
                        .Select(async x =>
                        {
                            var requestStopWatch = Stopwatch.StartNew();

                            var webClient = new WebClient();
                            using (var stream = await webClient.OpenReadTaskAsync(new Uri("http://www.riverbed.com")))
                            using (var sr = new StreamReader(stream))
                            {
                                var html = await sr.ReadToEndAsync();
                                htmlLength = html.Length;
                            }

                            requestTimes.Add(requestStopWatch.ElapsedMilliseconds);
                        });

                Task.WaitAll(tasks.ToArray());
            }
            catch (Exception e)
            {
                throw new Exception("Error getting http://www.riverbed.com: " + e.Message +
                                    Environment.NewLine + e.StackTrace);
            }

            logger.WriteMethodInfo(
                string.Format("Html Length [{0:n0}] chars.  Request Times: Min [{1:n0}] Avg [{2:n0}] Max [{3:n0}]",
                    htmlLength, requestTimes.Min(), requestTimes.Average(), requestTimes.Max()));

            logger.WriteMethodInfo("");
        }
Exemplo n.º 29
0
        public async Task<PackageSpec> GetPackageInfoAsync(PackageSpec entry, CancellationToken cancel) {
            string description = null;
            List<string> versions = null;

            lock (NotOnPyPI) {
                if (NotOnPyPI.Contains(entry.Name)) {
                    return new PackageSpec();
                }
            }

            using (var client = new WebClient()) {
                Stream data;
                try {
                    data = await client.OpenReadTaskAsync(new Uri(_index ?? DefaultIndex, entry.Name + "/json"));
                } catch (WebException ex) {
                    if ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) {
                        lock (NotOnPyPI) {
                            NotOnPyPI.Add(entry.Name);
                        }
                    }

                    // No net access or no such package
                    return new PackageSpec();
                }

                try {
                    using (var reader = JsonReaderWriterFactory.CreateJsonReader(data, new XmlDictionaryReaderQuotas())) {
                        var doc = XDocument.Load(reader);

                        // TODO: Get package URL
                        //url = (string)doc.Document
                        //    .Elements("root")
                        //    .Elements("info")
                        //    .Elements("package_url")
                        //    .FirstOrDefault();

                        description = (string)doc.Document
                            .Elements("root")
                            .Elements("info")
                            .Elements("description")
                            .FirstOrDefault();
                        
                        versions = doc.Document
                            .Elements("root")
                            .Elements("releases")
                            .Elements()
                            .Attributes("item")
                            .Select(a => a.Value)
                            .ToList();
                    }
                } catch (InvalidOperationException) {
                }
            }

            bool changed = false;
            PackageSpec result;

            using (await _cacheLock.LockAsync(cancel)) {
                if (!_cache.TryGetValue(entry.Name, out result)) {
                    result = _cache[entry.Name] = new PackageSpec(entry.Name);
                }

                if (!string.IsNullOrEmpty(description)) {
                    var lines = description.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
                    var firstLine = string.Join(
                        " ",
                        lines.TakeWhile(s => !IsSeparatorLine(s)).Select(s => s.Trim())
                    );
                    if (firstLine.Length > 500) {
                        firstLine = firstLine.Substring(0, 497) + "...";
                    }
                    if (firstLine == "UNKNOWN") {
                        firstLine = string.Empty;
                    }

                    result.Description = firstLine;
                    changed = true;
                }

                if (versions != null) {
                    var updateVersion = PackageVersion.TryParseAll(versions)
                        .Where(v => v.IsFinalRelease)
                        .OrderByDescending(v => v)
                        .FirstOrDefault();
                    result.ExactVersion = updateVersion;
                    changed = true;
                }
            }

            if (changed) {
                TriggerWriteCacheToDisk();
            }

            return result.Clone();
        }
Exemplo n.º 30
0
 /// <summary>
 /// Load image from web
 /// </summary>
 /// <param name="link"></param>
 /// <returns></returns>
 private async Task<Stream> LoadLinkAsync(string link)
 {
     try
     {
         var wc = new WebClient();
         Stream s = await wc.OpenReadTaskAsync(link);
         return s;
     }
     catch (Exception e)
     {
         return null;//we can't download it now
     }
 }
Exemplo n.º 31
0
 protected static async Task<XRefMap> DownloadFromWebAsync(Uri uri)
 {
     using (var wc = new WebClient())
     using (var stream = await wc.OpenReadTaskAsync(uri))
     using (var sr = new StreamReader(stream))
     {
         return YamlUtility.Deserialize<XRefMap>(sr);
     }
 }
		// Saves results to a local file - with user prompt for file name
		private async Task SaveResultsToFile(GPDataFile gpDataFile)
		{
			MessageBoxResult res = MessageBox.Show("Data file created. Would you like to download the file?", "Success", MessageBoxButton.OKCancel);
			if (res == MessageBoxResult.OK)
			{
				WebClient webClient = new WebClient();
				var stream = await webClient.OpenReadTaskAsync(gpDataFile.Uri);

				SaveFileDialog dialog = new SaveFileDialog();
				dialog.FileName = "Output.zip";
				dialog.Filter = "Zip file (*.zip)|*.zip";

				if (dialog.ShowDialog() == true)
				{
					try
					{
						using (Stream fs = (Stream)dialog.OpenFile())
						{
							stream.CopyTo(fs);
							fs.Flush();
							fs.Close();
						}
					}
					catch (Exception ex)
					{
						MessageBox.Show("Error saving file :" + ex.Message);
					}
				}
			}
		}
Exemplo n.º 33
0
        async void settingsPage_DefaultConfigurationDebugPage_ReadFileFromUri(string uri)
        {
            settingsPage_DefaultConfigurationDebugPage_progressring.Visibility = Visibility.Visible;
            settingsPage_DefaultConfigurationDebugPage_MainLabel.Visibility = Visibility.Collapsed;

            WebClient client = new WebClient();
            Stream stream = null;

            try
            {
                stream = await client.OpenReadTaskAsync(new Uri(uri));
            }
            catch (Exception ex)
            {
                TextMessageDialog("Could not grab configuration file", "Check your internet connection and try again.\nError: " + ex.Message);

                settingsPage_DefaultConfigurationDebugPage_MainLabel.Content = "Could not grab configuration file\n\n" + ex.Message;

                settingsPage_DefaultConfigurationDebugPage_progressring.Visibility = Visibility.Collapsed;

                return;
            }

            StreamReader reader = new StreamReader(stream);

            settingsPage_DefaultConfigurationDebugPage_MainLabel.Content = "";
            settingsPage_DefaultConfigurationDebugPage_MainLabel.Content = await reader.ReadToEndAsync();

            settingsPage_DefaultConfigurationDebugPage_progressring.Visibility = Visibility.Collapsed;
            settingsPage_DefaultConfigurationDebugPage_MainLabel.Visibility = Visibility.Visible;
        }
Exemplo n.º 34
0
        protected async Task<bool> CheckConnectionAsync()
        {
            try
            {

                using (var client = new WebClient())
                using (await client.OpenReadTaskAsync("http://www.google.com"))
                    return true;
            }
            catch
            {
                return false;
            }
        }