gpt4 book ai didi

c# - 使用 DownloadStringAsync 下载 HTML 源代码不会在 WP8 上下载整个源代码

转载 作者:可可西里 更新时间:2023-11-01 10:26:29 32 4
gpt4 key购买 nike

我正在尝试使用 DownloadStringAsync 下载一些 HTML 源代码。我的代码如下所示:

    WebClient client = new WebClient();

client.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(DownloadStringCallback2);
client.DownloadStringAsync(new Uri(url));

private void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e)
{
source = (string)e.Result;
if (!source.Contains("<!-- Inline markers start rendering here. -->"))
MessageBox.Show("Nope");
else
MessageBox.Show("Worked");
}

如果我查看变量“源”,我可以看到一些源在那里,但不是全部。但是,如果我这样做,它会起作用:

while (true)
{
source = wb.DownloadString(url);
if (source.Contains("<!-- Inline markers start rendering here. -->"))
break;
}

不幸的是,我不能使用这种方法,因为 WP8 没有 DownloadString。

有谁知道如何解决这个问题或者是否有更好的方法?

最佳答案

这个函数应该能帮到你

    public static Task<string> DownloadString(Uri url)
{
var tcs = new TaskCompletionSource<string>();
var wc = new WebClient();
wc.DownloadStringCompleted += (s, e) =>
{
if (e.Error != null) tcs.TrySetException(e.Error);
else if (e.Cancelled) tcs.TrySetCanceled();
else tcs.TrySetResult(e.Result);
};
wc.DownloadStringAsync(url);
return tcs.Task;
}

关于c# - 使用 DownloadStringAsync 下载 HTML 源代码不会在 WP8 上下载整个源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15671093/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com