2017-08-08 251 views
-2

我试图在Windows应用程序中下载一个zip文件,但正在抛出一个错误。我的代码是:无法下载文件 - 远程服务器返回一个错误:(403)禁止

string url = "https://www.nseindia.com/content/historical/DERIVATIVES/2017/JUN/fo07JUN2017bhav.csv.zip"; 
    using (WebClient wc = new WebClient()) 
    { 
     wc.DownloadFile(url, @"c:\bhav\file.zip"); 
    } 

异常详细信息:

System.Net.WebException was unhandled HResult=-2146233079
Message=The remote server returned an error: (403) Forbidden.
Source=System StackTrace: at System.Net.WebClient.DownloadFile(Uri address, String fileName) at System.Net.WebClient.DownloadFile(String address, String fileName) at unzip.Form1.downloadFile() in c:\users\ethicpro\documents\visual studio 2015\Projects\unzip\unzip\Form1.cs:line 30 at unzip.Form1..ctor() in c:\users\ethicpro\documents\visual studio 2015\Projects\unzip\unzip\Form1.cs:line 20 at unzip.Program.Main() in c:\users\ethicpro\documents\visual studio 2015\Projects\unzip\unzip\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel) at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext) at System.Activator.CreateInstance(ActivationContext activationContext) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

Link是:https://www.nseindia.com/content/historical/DERIVATIVES/2017/JUN/fo07JUN2017bhav.csv.zip

我搜索了其他的问题,但没有得到正确的答案。

+1

假设服务器正常使用的响应代码,这意味着你是不允许的。除非您没有提供正确的凭证,否则它可能与您的代码无关。 – Crowcoder

+0

正确的凭据,包括正确的用户代理,如果他们阻止类似刮板的代码。 –

+0

你还发现了什么其他问题?你如何提供你的证书?你可以用你的浏览器下载文件吗? – RedX

回答

1

试试这个

string url = "https://www.nseindia.com/content/historical/DERIVATIVES/2017/JUN/fo07JUN2017bhav.csv.zip"; 
     string fileName = @"C:\Temp\tt.zip"; 
     using (WebClient wc = new WebClient()) 
     { 
      wc.Headers.Add("User-Agent: Other"); 
      wc.DownloadFile(url, fileName); 
    } 
+0

谢谢,这很有帮助。 –

相关问题