Example #1
0
    public static void GetHighScores()
    {
        highScores.Clear();

        try
        {
        #if UNITY_WEBPLAYER
            WWW site = new WWW(_leaderboardUrl);

            while (!site.isDone)
            { }

            string jsonScores = site.text;
        #else
            WebClient webClient = new WebClient();

            webClient.DownloadDataCompleted += HandleDownloadDataCompleted;

            webClient.Headers.Add("Content-Type", "application/json");

            webClient.DownloadDataAsync(_leaderboardUri);
        #endif
        }
        catch(System.Exception ex)
        {
            Debug.Log(ex.Message);
        }
    }
 public static void DownloadDataAsync(this Uri uri, Action<byte[]> action)
 {
     if (uri == null)
         return;
     WebClient client = new WebClient();
     client.DownloadDataAsync(uri);
     client.DownloadDataCompleted += (sender, e) =>
     {
         action(e.Result);
     };
 }
Example #3
0
 public static async Task ConcurrentOperations_Throw()
 {
     await LoopbackServer.CreateServerAsync((server, url) =>
     {
         var wc = new WebClient();
         Task ignored = wc.DownloadDataTaskAsync(url); // won't complete
         Assert.Throws<NotSupportedException>(() => { wc.DownloadData(url); });
         Assert.Throws<NotSupportedException>(() => { wc.DownloadDataAsync(url); });
         Assert.Throws<NotSupportedException>(() => { wc.DownloadDataTaskAsync(url); });
         Assert.Throws<NotSupportedException>(() => { wc.DownloadString(url); });
         Assert.Throws<NotSupportedException>(() => { wc.DownloadStringAsync(url); });
         Assert.Throws<NotSupportedException>(() => { wc.DownloadStringTaskAsync(url); });
         Assert.Throws<NotSupportedException>(() => { wc.DownloadFile(url, "path"); });
         Assert.Throws<NotSupportedException>(() => { wc.DownloadFileAsync(url, "path"); });
         Assert.Throws<NotSupportedException>(() => { wc.DownloadFileTaskAsync(url, "path"); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadData(url, new byte[42]); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadDataAsync(url, new byte[42]); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadDataTaskAsync(url, new byte[42]); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadString(url, "42"); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadStringAsync(url, "42"); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadStringTaskAsync(url, "42"); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadFile(url, "path"); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadFileAsync(url, "path"); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadFileTaskAsync(url, "path"); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadValues(url, new NameValueCollection()); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadValuesAsync(url, new NameValueCollection()); });
         Assert.Throws<NotSupportedException>(() => { wc.UploadValuesTaskAsync(url, new NameValueCollection()); });
         return Task.CompletedTask;
     });
 }
Example #4
0
        // it's actually impossible to really show this old-style in a Windows Store app
        // the non-async APIs just don't exist :-)
        public void DownloadHomepage()
        {
            var webClient = new WebClient(); // not in Windows Store APIs

            webClient.DownloadStringCompleted += (sender, e) =>
            {
                if (e.Cancelled || e.Error != null)
                {
                    // do something with error
                }
                string contents = e.Result;

                int length = contents.Length;
                Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    ResultTextView.Text += "Downloaded the html and found out the length.\n\n";
                });
                webClient.DownloadDataCompleted += (sender1, e1) =>
                {
                    if (e1.Cancelled || e1.Error != null)
                    {
                        // do something with error
                    }
                    SaveBytesToFile(e1.Result, "team.jpg");

                    Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        ResultTextView.Text += "Downloaded the image.\n";

                        var img = ApplicationData.Current.LocalFolder.GetFileAsync("team.jpg");
                        var i = new BitmapImage(new Uri(img.Path, UriKind.Absolute));
                        DownloadedImageView.Source = i;
                    });

                    if (downloaded != null)
                        downloaded(length);
                };
                webClient.DownloadDataAsync(new Uri("http://xamarin.com/images/about/team.jpg"));
            };

            webClient.DownloadStringAsync(new Uri("http://xamarin.com/"));
        }
 protected void OnTestButtonClicked(object sender, EventArgs e)
 {
     testButton.State = Gtk.StateType.Insensitive;
     speedStatus.Text = "Testing... ";
     WebClient japan = new WebClient ();
     japan.Proxy = new WebProxy ("127.0.0.1:13370");
     japan.DownloadDataAsync (new Uri ("http://ipv4.download.thinkbroadband.com:8080/5MB.zip"));
     Stopwatch tmr = new Stopwatch ();
     tmr.Start ();
     double jSpeed;
     japan.DownloadDataCompleted += delegate(object sdr, DownloadDataCompletedEventArgs E) {
         if (true) {
             speedStatus.Text = "";
             jSpeed = (5000000 / 1024.0) / (tmr.ElapsedMilliseconds / 1000);
             GLib.Timeout.Add (200, updateStatus);
             theSpeed = jSpeed.ToString ().Substring (0, 6);
             theSpeed = "Result: " + Math.Round (jSpeed).ToString () + " KiB/s";
             tmr.Reset ();
             tmr.Stop ();
             japan.CancelAsync ();
             japan.CancelAsync ();
             japan.CancelAsync ();
             testButton.Sensitive = true;
         }
     };
 }
Example #6
-1
        public static void DownloadData_InvalidArguments_ThrowExceptions()
        {
            var wc = new WebClient();

            Assert.Throws<ArgumentNullException>("address", () => { wc.DownloadData((string)null); });
            Assert.Throws<ArgumentNullException>("address", () => { wc.DownloadData((Uri)null); });

            Assert.Throws<ArgumentNullException>("address", () => { wc.DownloadDataAsync((Uri)null); });
            Assert.Throws<ArgumentNullException>("address", () => { wc.DownloadDataAsync((Uri)null, null); });

            Assert.Throws<ArgumentNullException>("address", () => { wc.DownloadDataTaskAsync((string)null); });
            Assert.Throws<ArgumentNullException>("address", () => { wc.DownloadDataTaskAsync((Uri)null); });
        }