如何在此上下文中使用 WebClient.DownloadDataAsync() 方法?

2024-04-19

我的计划是让用户在我的程序中写下电影标题,我的程序将异步提取适当的信息,这样 UI 就不会冻结。

这是代码:

public class IMDB
    {
        WebClient WebClientX = new WebClient();
        byte[] Buffer = null;


        public string[] SearchForMovie(string SearchParameter)
        {
            //Format the search parameter so it forms a valid IMDB *SEARCH* url.
            //From within the search website we're going to pull the actual movie
            //link.
            string sitesearchURL = FindURL(SearchParameter);

            //Have a method download asynchronously the ENTIRE source code of the
            //IMDB *search* website.
            Buffer = WebClientX.DownloadDataAsync(sitesearchURL);


            //Pass the IMDB source code to method findInformation().

            //string [] lol = findInformation();

            //????

            //Profit.

            string[] lol = null;
            return lol;
        }

我的实际问题在于 WebClientX.DownloadDataAsync() 方法。我不能使用字符串 URL。我如何使用该内置函数来下载网站的字节(为了以后使用,我会将其转换为字符串,我知道如何做到这一点)而不冻结我的 GUI?

也许是 DownloadDataAsync 的清晰示例,以便我可以学习如何使用它?

谢谢你,你总是一个很棒的资源。


有一个较新的 DownloadDataTaskAsync 方法可让您等待结果。到目前为止,它更容易阅读并且更容易连接。我会用那个...

var client = new WebClient();

var data = await client.DownloadDataTaskAsync(new Uri(imageUrl));

await outstream.WriteAsync(data, 0, data.Length);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在此上下文中使用 WebClient.DownloadDataAsync() 方法? 的相关文章

随机推荐