0

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. 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!

Lacju
  • 91
  • 7
  • 3
    `does not work` - What does that mean? Nothing happens? An exception? – Equalsk Apr 13 '17 at 16:14
  • 1
    If you are getting an exception then you need to show us the log files that indicate what the exception is and where it is occuring. – takendarkk Apr 13 '17 at 16:14
  • See also [Is it possible to await async tasks during a button click?](http://stackoverflow.com/questions/17792745/is-it-possible-to-await-async-tasks-during-a-button-click) – Dustin Kingen Apr 13 '17 at 16:16
  • Sorry I should have been more clear. – Lacju Apr 13 '17 at 16:46

0 Answers0