1

This is a follow up from Download a file but it seems like I have to do it with a browser.

I feel the question was answered but I need additional help.

Here is my current code:

    public async void TryDownload()
    {
        var clientHandler = new HttpClientHandler
        {
            AllowAutoRedirect = true,
            UseCookies = true,
            CookieContainer = new CookieContainer()
        };

        using (var httpClient = new HttpClient(clientHandler))
        {
            // this gets the request and allows the site to set cookies.
            var warmup = await httpClient.GetAsync("https://www.fapiis.gov/fapiis/allfapiisdata.action"); //This is the last line that runs when I step thru it.

            // get the file (cookies are sent automatically).
            var fileResponse = httpClient.GetAsync("https://www.fapiis.gov/fapiis/downloadview?type=allFapiis");

            if (fileResponse.Result.IsSuccessStatusCode)
            {
                HttpContent content = fileResponse.Result.Content;
                var contentStream = await content.ReadAsStreamAsync();

                string fname = "allFapiis" + DateTime.Now.ToLongDateString() + ".xlsx";
                using (var fileStream = System.IO.File.Create(@"C:\ERA\DATA\FAPIIS\" + fname))
                {
                    contentStream.CopyTo(fileStream);
                }
            }
        }
    }    

    public void Main()
    {
        // TODO: Add your code here

        TryDownload();

        Dts.TaskResult = (int)ScriptResults.Success;
    }

I am running this in a script task in SSIS.

When I step through it, the process abruptly ends with setting of "warmup".

I can see that the process worked for the answerer because I can see that his output matches what I have downloaded manually.

If I try to incorporate await in the TryDownload() call from main it barks about needing to be in a async task method which I can't do because this is in main.

What am I doing wrong here?

Thanks for your help!

DerStarkeBaer
  • 669
  • 8
  • 28
KeithL
  • 5,348
  • 3
  • 19
  • 25
  • Have a look at [this](https://stackoverflow.com/a/9212343/9534819) – Matt.G Sep 26 '19 at 13:33
  • @Matt.G your link and this comment solved it: This was it for me. Language versions can also be set in the Properties page > Build Tab > Advanced . Setting that to 7.1 solved my issue even though I really don't understand what i did. – KeithL Sep 26 '19 at 13:53

1 Answers1

3

TryDownload should be a Task. Then you can await it.

public async Task TryDownload()

public static async void Main()
{
    await TryDownload();
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Program does not contain a static 'Main' method suitable for an entry point. This is probably just a setting I am unaware of changing. – KeithL Sep 26 '19 at 13:39
  • @KeithL [Error message “CS5001 Program does not contain a static 'Main' method suitable for an entry point”](https://stackoverflow.com/a/47588563/993547) – Patrick Hofman Sep 26 '19 at 13:44