I'm a little new to using async but I've discovered that I need it for reading a webpage and I can get the functionality to work flawlessly if I put the async methods right into button handler methods within my UI but I get vague exceptions if I try to move the async functionality into it's own class and access it from the button handler methods.
Examples:
Packaging my webscraping code in this class:
public class WebReader
{
public string completedSearchURL;
public string searchPageOutput;
public string completedProductPageURL;
public string productPageOutput;
public async Task QueryProductPage(string sku)
{
HttpClient httpClient = new HttpClient();
Uri searchURL = new Uri(("google.com/search" + SKU + "**additional URL content**"));
completedSearchURL = searchURL.ToString();
string productPageOutput;
try
{
searchPageOutput = await httpClient.GetStringAsync(searchURL);
}
catch (Exception)
{
throw;
}
searchPageOutput = searchPageOutput.Replace("</", "</" + System.Environment.NewLine);
Uri productPageUrl = new Uri(("www.google.com/" + BetweenTwoStrings(searchPageOutput)));
completedProductPageURL = productPageUrl.ToString();
try
{
productPageOutput = await httpClient.GetStringAsync(productPageUrl);
}
catch (Exception)
{
throw;
}
}
public string BetweenTwoStrings(string fullString)
{
string STR = fullString;
string firstString = "<a href=\"/****/*****/****/****/";
string secondString = "\" pid";
string finalString;
int Pos1 = STR.IndexOf(firstString) + firstString.Length;
int Pos2 = STR.IndexOf(secondString);
finalString = STR.Substring(Pos1, Pos2 - Pos1);
return finalString;
}
}
and attempting to access it like this:
private async void _testButtonQueryWeb_Click(object sender, RoutedEventArgs e)
{
WebReader wr = new WebReader();
await wr.QueryProductPage(_testTextInputBoxWebAddress.Text);
_testLinkDisplayPostSearch.Text = wr.searchPageOutput;
_testFullyConstructedLink.Text = wr.completedSearchURL;
_testTextBoxWeb.Text = wr.productPageOutput;
}
does not work.
However using the code like this will:
private async void _testButtonQueryWeb_Click(object sender, RoutedEventArgs e)
{
string completedSearchURL;
string searchPageOutput;
string completedProductPageURL;
string productPageOutput;
HttpClient httpClient = new HttpClient();
Uri searchURL = new Uri(("google.com/search" + _testTextInputBoxWebAddress + "**additional URL content**"));
completedSearchURL = searchURL.ToString();
try
{
searchPageOutput = await httpClient.GetStringAsync(searchURL);
}
catch (Exception)
{
throw;
}
searchPageOutput = searchPageOutput.Replace("</", "</" + System.Environment.NewLine);
Uri productPageUrl = new Uri(("www.google.com/" + BetweenTwoStrings(searchPageOutput)));
completedProductPageURL = productPageUrl.ToString();
try
{
productPageOutput = await httpClient.GetStringAsync(productPageUrl);
}
catch (Exception)
{
throw;
}
_testLinkDisplayPostSearch.Text = searchPageOutput;
_testFullyConstructedLink.Text = completedSearchURL;
_testTextBoxWeb.Text = productPageOutput;
}
The application runs fine until I hit the Query Web button and then it throws this exception.
This is the error message from the log file : Exception thrown: 'System.IO.FileLoadException' in System.Private.CoreLib.ni.dll
Needless to say I'm a little lost and frustrated and I'd really appreciate a little guidance on what I might be doing wrong and more so why techniques that, from what I've learned so far about programming should work in this case don't.
Thanks in advance!