Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Developers,

I want to download a bhav copy from www.nseindia.com.

I tried the following code but it gives the error.

VB
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Try
            Dim wc As New WebClient
            Dim savefile As New SaveFileDialog
            Dim fileurl As String = "www.nseindia.com/content/historical/EQUITIES/2012/AUG/cm28AUG2012bhav.csv.zip"
            Dim filelocation As String = "c:\cm28AUG2012bhav.csv.zip"
            wc.DownloadFile(fileurl, filelocation)
          
        Catch exception As FileTransferException
            MsgBox("CAnnot Download")
        End Try

    End Sub

It is not taking the url without "http:\\"

The url specified in the fileurl is working if it is pasted in browser.But in the code it is giving error.

Please specific the solution for the same.

Thanks & Regards.
Posted
Updated 28-Aug-12 6:01am
v2
Comments
[no name] 28-Aug-12 12:02pm    
It's really very helpful to know what the exact error is....
Christian Graus 28-Aug-12 12:06pm    
The error means what it says, but we don't know what it says.
bbirajdar 28-Aug-12 14:06pm    
Are you a software developer ? Can you solve somebody else's problem if he comes to you and says ' it gives error' ?

You need to pass the alid header to work.

I have checked the nse url and found two headers you must need to add to allow the remote server to accept your download,

wc.Headers("Accept") =    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"

wc.Headers("User-Agent") = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1"


Adding these 2 lines just before your DownloadFile will definitely work.

Good luck.
cheers
 
Share this answer
 
Comments
vinay.sarmalkar 28-Aug-12 13:25pm    
DEAR SANDIP,
I JUST TRIED THE SAME BUT STILL NOT RESULT.
CAN U TELL ME EXACTLY WHAT URL I HAVE TO SPECIFY MEANS URL WITHOUT "HTTP://" WHICH I HAVE SPECIFIED IN MY QUESTION OR URL WITH "HTTP://"?

THANKS & REGARDS.
Sandip.Nascar 28-Aug-12 13:36pm    
you need to put http:// other wise the url cannot be recognized.

Here is the code,
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
Dim wc As New WebClient

Dim fileurl As String = "http://www.nseindia.com/content/historical/EQUITIES/2012/AUG/cm28AUG2012bhav.csv.zip"
Dim filelocation As String = "d:\aaaaa.zip"

wc.Headers("Accept") = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
wc.Headers("User-Agent") = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1"

wc.DownloadFile(fileurl, filelocation)

Catch exception As Exception
MsgBox("CAnnot Download")
End Try
End Sub
kunaljvyas 6-Apr-19 5:05am    
hi @Sandip.Nascar, thanks a lot for sharing this code. i was trying to use your suggested code.
its working for dates only after 2016-jan-01. not for any files prior to this date. i tried fiddler and changing the headers. but not working.
download manager can still download with the same link. pls help to solve this.
Sandip.Nascar 28-Aug-12 13:38pm    
if this is not working, trap the error, if it is not 403, check whether your C: drive has a write permission.
vinay.sarmalkar 28-Aug-12 15:19pm    
Thanxs Bro..it's working fine..Thanxs alot for the help..

Please let me know where i can find the information for the below mention coding lines:

wc.Headers("Accept") = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"

wc.Headers("User-Agent") = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1"

Thanks & Regards
After trying your code which is correct you need to look at the exception that is raised

The remote server returned an error: (403) Forbidden.


looks like you can't download it in this way.
 
Share this answer
 
use default credentials.

C#
wc.UseDefaultCrdentials = True


thanks,

Kuthuparakkal
 
Share this answer
 
Comments
vinay.sarmalkar 28-Aug-12 13:27pm    
DEAR Kuthuparakkal,

I TRIED YOUR SOLUTION TOO BUT IT ALSO NOT WORKING.

PLEASE TELL ME AFTER WHICH LINE I HAVE TO SPECIFY THE ABOVE CODE LINE & WHAT URL I HAVE TO SPECIFY MEANS URL WITHOUT "HTTP://" WHICH I HAVE SPECIFIED IN MY QUESTION OR URL WITH "HTTP://"?

THANKS & REGARDS.
it should work

VB
clnt.Proxy = New WebProxy("http://nseindia.com", True)

clnt.Headers("Accept") = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"

clnt.Headers("User-Agent") = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1"
clnt.DownloadFile(fileurl, DATAFILEDIR & "/temp/" & outfilename)
 
Share this answer
 
Comments
CHill60 12-Jan-15 14:38pm    
Please don't post solutions to old posts that have already been resolved

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