简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM