Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This works:
C++
URLDownloadToFile, https://www.nseindia.com/content/historical/EQUITIES/2016/JAN/cm01JAN2016bhav.csv.zip, C:\Users\Dave\Desktop\Happy.zip

But I donot understand why this does not even if the URL syntax or format is same:
C++
URLDownloadToFile, https://www.nseindia.com/content/historical/EQUITIES/2015/DEC/cm31DEC2015bhav.csv.zip, C:\Users\Dave\Desktop\Sad.zip


What I have tried:

I found the below method written in C# from social.msdn for the same issue but my AHK skills are very limited. Could you help convert the below C# to AHK?
C#
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.Headers.Add("Content-Type", "application / zip, application / octet - stream");
objWebClient.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
objWebClient.Headers.Add("Referer", "http://Something");
objWebClient.Headers.Add("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
objWebClient.DownloadFile(uriWebClient, filepath);
Posted
Updated 23-Nov-18 2:11am

Because the second URL returns 403: Forbidden.

That response status means you are not allowed to download that file, even if you were authenticated.

List of HTTP status codes - Wikipedia[^]
HTTP 403 - Wikipedia[^]

EDIT: Looks like the server is validating the referrer header. Change it to:
objWebClient.Headers.Add("Referer", "https://www.nseindia.com/products/content/equities/equities/archieve_eq.htm");
 
Share this answer
 
v2
Comments
ChimpTrader 23-Nov-18 7:52am    
Thanks for your interest!
However, I am able to download the file from the website manually.
Link: NSE - National Stock Exchange of India Ltd.
Date(DD-MM-YYYY): 12-31-2015

Objective is to download and collect such CSVs for each day of past 10 years. So, I do not want to use IE COM, as that will make the script extremely slow.

Any other way, Richard? Thanks again....

P.S.
Please visit
https://social.msdn.microsoft.com/Forums/vstudio/en-US/31a7deb0-b135-4163-b884-3efa35ba6994/webclient-403-forbidden?forum=csharpgeneral

The code is C#, any way to get the same result in AHK?
Richard Deeming 23-Nov-18 7:57am    
Looks like the server is validating the referrer header. You need to change it to:
objWebClient.Headers.Add("Referer", "https://www.nseindia.com/products/content/equities/equities/archieve_eq.htm");
Thanks Richard,
I found this StackOverflow page.
Using the Download method and your suggestion, able to get the desired outcome.
C++
Download( UrlToFile, SaveFileAs:="", Overwrite := True, headers := "", method := "GET", postData := "")
{
	 MsgBox, %headers%
     WinHttpObj := ComObjCreate( "WinHttp.WinHttpRequest.5.1" )
     WinHttpObj.Open( method, UrlToFile )
     For header, value in headers 
         WinHttpObj.SetRequestHeader( header, value )
     WinHttpObj.Send( postData )

     ADODBObj := ComObjCreate( "ADODB.Stream" )
     ADODBObj.Type := 1
     ADODBObj.Open()
     ADODBObj.Write( WinHttpObj.ResponseBody )
     If !SaveFileAs
	 {
         urlSplitArray := StrSplit( UrlToFile, "/" )
         SaveFileAs := urlSplitArray[ urlSplitArray.MaxIndex() ]
     }        
     ADODBObj.SaveToFile( SaveFileAs, Overwrite ? 2:1 )
     ADODBObj.Close()
}

customHeaders := { "User-Agent": "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
				 ,"Content-Type": "application / zip, application / octet - stream""application / zip, application / octet - stream"
				 ,"Accept-Encoding": "gzip,deflate,sdch"
				 ,"Referer": "https://www.nseindia.com/products/content/equities/equities/archieve_eq.htm"
				 ,"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" }

Download( "https://www.nseindia.com/content/historical/EQUITIES/2015/DEC/cm31DEC2015bhav.csv.zip", "C:\Users\Dave\Desktop\Happy.zip", True, customHeaders )
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900