C# 将 WebClient 方法转换为 async/await
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13240915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a WebClient method to async / await
提问by ColinE
I have some existing code which I am porting to Windows 8 WinRT. The code fetches data from URL, asynchronously invoking a passed delegate:
我有一些现有的代码,我正在将它们移植到 Windows 8 WinRT。代码从 URL 获取数据,异步调用传递的委托:
private void RequestData(string uri, Action<string> action)
{
var client = new WebClient();
client.DownloadStringCompleted += (s,e) => action(e.Result);
client.DownloadStringAsync(new Uri(uri));
}
Converting to WinRT requires the use of HttpClientand asynchronous methods. I've read a few tutorials on async / await, but am a bit baffled. How can I change the method above, but maintain the method signature in order to avoid changing much more of my code?
转换为 WinRT 需要使用HttpClient异步方法。我已经阅读了一些关于 async/await 的教程,但我有点困惑。如何更改上述方法,但保持方法签名以避免更改更多代码?
采纳答案by Cole Cameron
private async void RequestData(string uri, Action<string> action)
{
var client = new WebClient();
string data = await client.DownloadStringTaskAsync(uri);
action(data);
}
回答by L.B
var client = new WebClient();
string page = await client.DownloadStringTaskAsync("url");
or
或者
var client = new HttpClient();
string page = await client.GetStringAsync("url");
回答by McGarnagle
Following this example, you first create the async task wtih, then get its result using await:
按照此示例,您首先使用以下方法创建异步任务,然后使用await以下命令获取其结果:
Task<string> downloadStringTask = client.DownloadStringTaskAsync(new Uri(uri));
string result = await downloadStringTask;
回答by dtb
awaitthe result of the HttpClient.GetStringAsync Method:
awaitHttpClient.GetStringAsync 方法的结果:
var client = new HttpClient();
action(await client.GetStringAsync(uri));
回答by Stephen Cleary
How can I change the method above, but maintain the method signature in order to avoid changing much more of my code?
如何更改上述方法,但保持方法签名以避免更改更多代码?
The best answer is "you don't". If you use async, then use it all the way down.
最好的答案是“你没有”。如果您使用async,则一直使用它。
private async Task<string> RequestData(string uri)
{
using (var client = new HttpClient())
{
return await client.GetStringAsync(uri);
}
}
回答by Srost
And this piece of code is for UploadValuesAsync:
这段代码用于 UploadValuesAsync:
public class WebClientAdvanced : WebClient
{
public async Task<byte[]> UploadValuesAsync(string address, string method, IDictionary<string, string> data)
{
var nvc = new NameValueCollection();
foreach (var x in data) nvc.Add(x.Key, x.Value.ToStr());
var tcs = new TaskCompletionSource<byte[]>();
UploadValuesCompleted += (s, e) =>
{
if (e.Cancelled) tcs.SetCanceled();
else if (e.Error != null) tcs.SetException(e.Error);
else tcs.SetResult(e.Result);
};
UploadValuesAsync(new Uri(address), method, nvc);
var result = await tcs.Task;
return result;
}
}

