private async void btnRead_Click(object sender, EventArgs e) { if (String.IsNullOrWhiteSpace(tbURL.Text)) { MessageBox.Show("The URL must not be empty"); return; } tbURL.Enabled = false; btnRead.Enabled = false; try { Product = await Yuppo.ReadProductTaskAsync(tbURL.Text); tbOrgName.Text = Product.Name; tbAlbumName.Text = Product.Name; tbAlbumName.Enabled = true; btnDone.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static async Task<Product> ReadProductTaskAsync(string url) { using (WebClient client = new WebClient()) { client.Encoding = Encoding.UTF8; client.Headers.Add("Accept-Language", "en-US,en;q=0.5"); String content = await DownloadStringTaskAsync(client, UpgradeUri(url), NUMBER_OF_TRIES); Product prod = new Product(); Match m1 = reProductName.Match(content); if (m1.Success) { prod.Name = m1.Groups[1].Value; } else { throw new Exception("Missing product name"); } Match mNext = null; do { // read photos MatchCollection mc = rePhotos.Matches(content); foreach (Match m2 in mc) { Photo photo = new Photo() { Url = m2.Groups[1].Value.Replace("square.jpg", "big.jpg"), Name = m2.Groups[2].Value }; prod.Photos.Add(photo); } // check existence of the next page mNext = reNext.Match(content); if (mNext.Success) { // fetch contents of the next page content = await DownloadStringTaskAsync(client, UpgradeUri("http://v.yupoo.com" + mNext.Groups[1].Value), NUMBER_OF_TRIES); } } while (mNext.Success); return prod; } }