异步Web响应的Windows Phone 8 [英] asynchronous web response windows phone 8
问题描述
这是code我用它来下载网址为HTML。问题是,有没有什么办法可以做到
这个异步?的问题,我想知道该响应是否是成功的,哪些是在响应程序继续之前。这将是完美的,如果你能等待client.DownloadStringAsync和task.delay并不总是可行的,除了我不喜欢设定一个标准的时间去等待的这个想法。谢谢!
开放的URI =新的URI(URL);
Web客户端的客户端=新的WebClient();
client.DownloadStringCompleted + =新DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.AllowReadStreamBuffering = TRUE;
client.DownloadStringAsync(URI);
有两种解决方案。
第一个(我建议)是使用 Microsoft.Net.Http的NuGet包,改变你的code使用新的的HttpClient
,而不是旧 Web客户端
:
开放的URI =新的URI(URL);
HttpClient的客户端=新的HttpClient();
等待client.GetStringAsync(URI);
第二个解决方案将允许您 Web客户端
继续使用,因此它可以作为临时的解决办法,如果你有这取决于<$其他很多code的工作C $ C> Web客户端。要使用异步
与 Web客户端
,安装的 Microsoft.Bcl.Async的NuGet包,然后你可以使用 DownloadStringTaskAsync
:
开放的URI =新的URI(URL);
Web客户端的客户端=新的WebClient();
等待client.DownloadStringTaskAsync(URI);
This is the code I use to download an URL as html. The thing is, is there any way I can do this asynchronous? the problem I want to know whether the response is successful and what is in the response before the program continues. It would be perfect if you could await client.DownloadStringAsync and a task.delay won't always work besides I don't like that idea of setting an standard time to wait. Thank you!
Uri uri = new Uri(url);
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.AllowReadStreamBuffering = true;
client.DownloadStringAsync(uri);
There are two solutions.
The first one (which I recommend) is to use the Microsoft.Net.Http NuGet package and change your code to use the new HttpClient
instead of the old WebClient
:
Uri uri = new Uri(url);
HttpClient client = new HttpClient();
await client.GetStringAsync(uri);
The second solution will allow you to continue using WebClient
, so it may work as a temporary fix if you have a lot of other code depending on WebClient
. To use async
with WebClient
, install the Microsoft.Bcl.Async NuGet package, and then you can use DownloadStringTaskAsync
:
Uri uri = new Uri(url);
WebClient client = new WebClient();
await client.DownloadStringTaskAsync(uri);
这篇关于异步Web响应的Windows Phone 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!