質問

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?

役に立ちましたか?

解決

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

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top