2017-08-08 48 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:

リンクです: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

ありがとう、それは役に立ちます。 –

関連する問題