DingoCC
DingoCC

Reputation: 159

WebClient.DownloadFile requires what exactly for the URI?

I'm good with the code, it works great for other solutions of mine. I have a knowledge gap as I do not understand what constitutes a URI. This should work, but does not:

https://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download

Now I'm thinking that this is not a file right? Throwing the above at a browser provides a file though. The exception message is "The underlying connection was closed: An unexpected error occurred on a receive."

        String address = "https://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download";

.....

        using (WebClient Client = new WebClient())
        {
            try
            {
                Client.DownloadFile(address, destPath + filename);
            }
            catch (Exception ex)
            {
                Log.Line("Error: " + ex.Message);
                return 1;
            }
        }

The URI: this link

Upvotes: 3

Views: 216

Answers (1)

Markus Dresch
Markus Dresch

Reputation: 5574

You've got a perfectly valid URI. The target server may respond to requests in a different way than you expect though. For example depending on your web client. To debug issues like this use curl.

curl -v https://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nasdaq&render=download

The above command shows you that the server does not reply with the expected csv file. That's not a problem in your code. You can try to pretend a different user agent using the curl -H flag or set some redirection options until you get there.

In your specific case it seems to be the header Accept-Encoding: gzip that solves the issue.

Upvotes: 1

Related Questions