I am trying to download a file from a website using the following code:
WebClient webClient = new WebClient();
webClient.DownloadFile("http://www.nseindia.com/content/historical/EQUITIES/2011/MAR/cm07MAR2011bhav.csv.zip", @"c:\myfile.txt");
The exception shown is "forbidden error 403"
It means page not found but I can download that file using java code and also I can directly download it from that website.
How do I download this using C# code?
The first thing to notice is that if you try the URL in your browser, the file does download. What that tells you is that you need to configure the WebClient to send headers mimicking what the website would expect a browser to do. This is what works for me:
var wc = new WebClient();
var ua = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
wc.Headers.Add(HttpRequestHeader.UserAgent, ua);
wc.Headers["Accept"] = "/";
wc.DownloadFile("http://www.nseindia.com/content/historical/EQUITIES/2011/MAR/cm07MAR2011bhav.csv.zip", @"d:\myfile.txt");
As an aside, saving to the C: root is problematic. Save somewhere else.
I tested that URL with wget, and got a 403 error. I was able to solve that problem by adding a user-agent string to the header
Try adding a user-agent string to the header, using webClient.Headers.Add(HttpRequestHeader.UserAgent, "blah")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With