Reputation: 936
what i am trying to do is go to this (http://www.dubstep.net/track/5439) loop through the html looking for href. once it finds a # it takes the href url before the #. and then down load the file from that url before the #. now the code below does everything up till downloading. now how would i download the file from url t?
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);
}
}
}
}
below is code that will download the file i want, but this is in a console application.. how would i achieve this?
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");
}
}
Upvotes: 0
Views: 673
Reputation: 31724
The way I've usually been doing it is with a BackgroundDownloader
which might be an overkill, but in case of a simple html file you can download a string - just call
string htmlString = await new HttpClient().GetStringAsync(uri);
...then you should be able to load it with something like htmlDocument.Load(htmlString);
Upvotes: 1