Search code examples
c#.netdownloadwebclient

Download file with c# WebClient


I'm trying to download a csv file using c# webclient from this link:

http://www.tase.co.il/_layouts/Tase/ManagementPages/Export.aspx?sn=none&enumTblType=allShares&Columns=noneColumns&Titles=noneTitles&action=1&SubAction=0&GridId=33&CurGuid={26F9CCE6-D184-43C6-BAB9-CF7848987BFF}&ExportType=3

This link works in my browser fine; however it doesn't work when I use the following code

WebClient ta = new WebClient();
ta.DownloadFileAsync(new Uri("http://www.tase.co.il/_layouts/Tase/ManagementPages/Export.aspx?sn=none&enumTblType=allShares&Columns=noneColumns&Titles=noneTitles&action=1&SubAction=0&GridId=33&CurGuid={26F9CCE6-D184-43C6-BAB9-CF7848987BFF}&ExportType=3"), "s.csv");

I get an empty s.csv file size: 0 bytes. What can I do?


Solution

  • I gave your code a try and it returned the same results for me. I registered for the "DownloadProgressChanged" event and could see an exception:

    System.Net.WebException: The remote server returned an error: (403) Forbidden.

    Adding a user-agent header based on the following link resolves the problem:

    WebClient - The remote server returned an error: (403) Forbidden