本文整理汇总了C#中WebClient.DownloadDataAsync方法的典型用法代码示例。如果您正苦于以下问题:C# WebClient.DownloadDataAsync方法的具体用法?C# WebClient.DownloadDataAsync怎么用?C# WebClient.DownloadDataAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WebClient
的用法示例。
在下文中一共展示了WebClient.DownloadDataAsync方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetHighScores
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);
}
}
示例2: DownloadDataAsync
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);
};
}
示例3: ConcurrentOperations_Throw
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;
});
}
示例4: DownloadHomepage
// 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/"));
}
示例5: OnTestButtonClicked
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;
}
};
}
示例6: DownloadData_InvalidArguments_ThrowExceptions
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); });
}