0
我想要做的是通過html尋找href去這(http://www.dubstep.net/track/5439)循環。一旦找到#,它會在#之前使用href url。然後在#之前從該URL下載該文件。現在下面的代碼會一直做下去。現在我將如何從url t下載文件?如何從網上下載文件,Windows Store應用程序
public async void songsLoad()
{
var messageDialog = new MessageDialog("1");
await messageDialog.ShowAsync();
//use HAP's HtmlWeb instead of WebClient
var htmlweb = new HtmlWeb();
// load HtmlDocument from web URL
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = await htmlweb.LoadFromWebAsync("http://www.dubstep.net/track/5439");
int i = 0;
List<string> list = new List<string>();
//use LINQ API to select all `<a>` having `href` attribute
var links = doc.DocumentNode
.DescendantsAndSelf("a")
.Where(o => o.GetAttributeValue("href", null) != null);
foreach (HtmlNode link in links)
{
HtmlAttribute href = link.Attributes["href"];
if (href != null)
{
list.Add(href.Value);
i++;
if (href.Value == "#")
{
int t = i - 2;
Uri test = new Uri(list[t]);
start(test);
}
}
}
}
下面
是代碼會下載我想要的文件,但是這是在控制檯應用程序.. 如何將我做到這一點?
public static void start(Uri t)
{
string fileName1 = "t", myStringWebResource = null;
// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
myWebClient.DownloadFileCompleted += DownloadCompleted;
myWebClient.DownloadProgressChanged += myWebClient_DownloadProgressChanged;
myWebClient.DownloadFileAsync(t, "file.mp3");
}
}
你是說'LoadFromWebAsync()'不起作用? –
如果這不起作用,你可以嘗試一個普通的'HttpClient'。 –
@FilipSkakun不,但你可以舉一個如何使用它的例子嗎? 也生病看着httpclient – hurnhu