2017-08-08 152 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

謝謝,這很有幫助。 –

相關問題