0

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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
krukita
  • 89
  • 1
  • 10

2 Answers2

2

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Brian Booth
  • 727
  • 1
  • 6
  • 10
-1

This worked for similar issue:

WebClient.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)");

https://stackoverflow.com/a/2496982/445533

Edit: Header example linked in accepted answer is not properly formatted and may fail.

bretddog
  • 5,411
  • 11
  • 63
  • 111
  • You copied another answer as a new answer. IMO this should be a comment, not an answer. In addition, this seems to be the same kind of answer as the accepted one. – Uwe Keim Dec 01 '20 at 12:13
  • The accepted answer links to a header example that is not properly formatted. It failed in my case, while the one I posted worked. Hence this answer should be helpful. – bretddog Dec 01 '20 at 12:33