private void FetchDataAdd() { Regex urlRegex = new Regex(@"(https|http)://(www.)?.+?\.\w+"); Regex proshopUrlRegex = new Regex(@"(https|http)://(www.)?proshop\.dk"); Regex komplettUrlRegex = new Regex(@"^(https|http)://(www.)?komplett\.dk"); if (!string.IsNullOrEmpty(AddProductUrlTextBox.Text)) { if (urlRegex.IsMatch(AddProductUrlTextBox.Text)) { if (proshopUrlRegex.IsMatch(AddProductUrlTextBox.Text) || komplettUrlRegex.IsMatch(AddProductUrlTextBox.Text)) { var dl = new DownloadData(); string urlFromUrlTextBox = AddProductUrlTextBox.Text; var rawHtml = dl.FetchRawHtml(urlFromUrlTextBox); if (rawHtml == string.Empty) { MessageBox.Show("Cannot connect to \"" + urlFromUrlTextBox + "\""); } else if (proshopUrlRegex.IsMatch(AddProductUrlTextBox.Text)) { AssignTexboxesProshop(rawHtml); } else if (komplettUrlRegex.IsMatch(AddProductUrlTextBox.Text)) { AssignTexboxesKomplett(rawHtml); } } else { MessageBox.Show("Please provide a link to a supported website."); } } else { MessageBox.Show("Please provide a valid URL with \"http://\"."); } } else { MessageBox.Show("Please enter a URL."); } }
public async void FetchData() { Regex urlRegex = new Regex(@"(https|http)://(www.)?.+?\.\w+"); Regex proshopUrlRegex = new Regex(@"(https|http)://(www.)?proshop\.dk"); Regex komplettUrlRegex = new Regex(@"^(https|http)://(www.)?komplett\.dk"); if (!string.IsNullOrEmpty(PUrlTxtBox.Text)) { if (urlRegex.IsMatch(PUrlTxtBox.Text)) { testTextBox.Text = urlRegex.Match(PUrlTxtBox.Text).ToString(); if (proshopUrlRegex.IsMatch(PUrlTxtBox.Text) || komplettUrlRegex.IsMatch(PUrlTxtBox.Text)) { var dl = new DownloadData(); string urlFromUrlTextBox = PUrlTxtBox.Text; var downloadRawHtml = Task <string> .Factory.StartNew(() => dl.FetchRawHtml(urlFromUrlTextBox)); TestLabel.Content = "Waiting"; FetchDataBtn.IsEnabled = false; await downloadRawHtml; //TODO Evt. have mulighed for at cancel download med en knap oven på Fetch Data. FetchDataBtn.IsEnabled = true; var rawHtml = downloadRawHtml.Result; TestLabel.Content = "Done"; if (rawHtml == string.Empty) { MessageBox.Show("Cannot connect to \"" + urlFromUrlTextBox + "\""); } else if (proshopUrlRegex.IsMatch(PUrlTxtBox.Text)) { AssignTexboxesProshop(rawHtml); } else if (komplettUrlRegex.IsMatch(PUrlTxtBox.Text)) { AssignTexboxesKomplett(rawHtml); } } else { MessageBox.Show("Please provide a link to a supported website."); } } else { MessageBox.Show("Please provide a valid URL with \"http://\"."); } } else { MessageBox.Show("Please enter a URL."); } }