I am trying to write some code to download file from an url. But this seemingly trivial function doesn't work. I have explored lot of variations (based on some earlier posts): url encoding, user agent, content type, stream based downloads etc. and nothing seems to work (while the url works perfect with Internet Explorer).
url="http://www.nseindia.com/content/historical/DERIVATIVES/2013/NOV/fo14NOV2013bhav.csv.zip"
public void NewDowloadFile(string url, string filepath) { WebClient objWebClient = new WebClient(); Uri uriWebClient = new Uri(url); objWebClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); objWebClient.Credentials = System.Net.CredentialCache.DefaultCredentials; objWebClient.Headers.Add("Content-Type", "application / zip, application / octet - stream"); objWebClient.DownloadFile(uriWebClient, filepath); }
I used Fiddler and could see the following response:
<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD><BODY><H1>Access Denied</H1> You don't have permission to access "http://www.nseindia.com/content/historical/DERIVATIVES/2013/NOV/fo14NOV2013bhav.csv.zip" on this server.<P> Reference #18.14c7387d.1385735320.33b58ded</BODY></HTML>
I am really puzzled, any help highly appreciated.
Thanks
Thanks - Mukesh