Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a file from a website using C#

Tags:

c#

.net

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?

like image 634
RAHUL Avatar asked Mar 08 '11 05:03

RAHUL


2 Answers

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.

like image 166
Ritch Melton Avatar answered Sep 26 '22 00:09

Ritch Melton


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")

like image 22
BernzSed Avatar answered Sep 26 '22 00:09

BernzSed