8

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?

Justin
  • 84,773
  • 49
  • 224
  • 367
RAHUL
  • 127
  • 2
  • 8
  • 6
    403 Forbidden does not mean page not found..it means forbidden – BrokenGlass Mar 08 '11 at 05:11
  • 403 does not mean page not found; that would be 404. 403 actually is `Forbidden', like the contents are there but you don't have the right credentials. – Bala R Mar 08 '11 at 05:13
  • i can download it directly from that website and also using java code.but using the c#code i cant download – RAHUL Mar 08 '11 at 05:16

5 Answers5

6

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.

Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
3

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

BernzSed
  • 1,129
  • 8
  • 9
2

You need to set the following two headers for this to work:

  • User-Agent: set to just some standard browser user-agent
  • Accept: set to accept "application/zip"

Example (tested):

WebClient webClient = new WebClient();
webClient.Headers.Add("Accept", "application/zip");
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
webClient.DownloadFile("http://www.nseindia.com/content/historical/EQUITIES/2011/MAR/cm07MAR2011bhav.csv.zip", @"D:\test\test.zip");
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • @BrokenGlass: out of curiosity, do you happen to know the default user-agent of WebClient? – Brad Christie Mar 08 '11 at 05:24
  • By default that header is not set at all if I'm not mistaken – BrokenGlass Mar 08 '11 at 05:25
  • 1
    @Brad, its nothing. A good way to debug/peer into HTTP interactions is to use a project called fiddler. http://www.fiddler2.com/fiddler2/. The only thing sent is the GET request. – Ritch Melton Mar 08 '11 at 05:28
  • @RitchMelton: I usually use ethereal, but figured someone would have a faster answer than me spinning up a project. Thanks for the answer, I appreciate it. – Brad Christie Mar 08 '11 at 05:30
  • @Brad - Based on my experience with deploying distributed satcom WANs, I think Etheral (WireShark, etc..) are good for troubleshooting network issues. Fiddler is great for application level HTTP debugging. Its a specific tool for a specific job. I highly recommend taking a look. – Ritch Melton Mar 08 '11 at 05:34
  • @RitchMelton: Will do, though these days I'm not doing too much debugging in that field (aside from helping an SO-er ever so often). And anything I do perform is usually easily debugged with TamperData in FF (though back when I did do it, i usually liked seeing the raw data--was always fun making game hacks via proxy back in the 90s ;p--oops, did I say that?) – Brad Christie Mar 08 '11 at 05:36
0
using (WebClient wc = new WebClient())
{
  wc.Headers.Add("Referer:https://www.nseindia.com/products/content/equities/equities/archieve_eq.htm");
  wc.Headers.Add("User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
  wc.DownloadFile(url, fileName);
}
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
Raz
  • 1
  • 1
-2

Try something like

WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
Raj
  • 1,742
  • 1
  • 12
  • 17