从指定的URL下载文件 [英] Downloading a file from specified URL
问题描述
亲爱的开发者,
我想从www.nseindia.com下载bhav副本。
我尝试了以下代码,但它给出了错误。
Dear Developers,
I want to download a bhav copy from www.nseindia.com.
I tried the following code but it gives the error.
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
这不是网址没有http:\\
如果将文件夹粘贴在浏览器中,则文件中指定的URL正常工作。但是在代码中它给出了错误。
请具体说明相同的解决方案。
谢谢&问候。
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.
推荐答案
你需要传递alid标题才能工作。
我已经检查过了url并找到了两个标题,你必须添加它才能让远程服务器接受你的下载,
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"
添加这些2好吧,你的DownloadFile肯定会有效。
祝你好运。
欢呼
Adding these 2 lines just before your DownloadFile will definitely work.
Good luck.
cheers
尝试后您的代码是正确的,您需要查看引发的异常
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.
使用默认凭证。
use default credentials.
wc.UseDefaultCrdentials = True
谢谢,
Kuthuparakkal
thanks,
Kuthuparakkal
这篇关于从指定的URL下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!