Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法下载文件-远程服务器返回错误:(403)禁止_C# - Fatal编程技术网

C# 无法下载文件-远程服务器返回错误:(403)禁止

C# 无法下载文件-远程服务器返回错误:(403)禁止,c#,C#,我正试图在Windows应用程序中下载zip文件,但正在抛出一个错误。我的代码是: 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");

我正试图在Windows应用程序中下载zip文件,但正在抛出一个错误。我的代码是:

    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未经处理HResult=-2146233079
Message=远程服务器返回错误:(403)禁止。
Source=系统堆栈跟踪: 位于System.Net.WebClient.DownloadFile(Uri地址,字符串文件名) 位于System.Net.WebClient.DownloadFile(字符串地址、字符串文件名) 在c:\users\ethicpro\documents\visual studio中的unzip.Form1.downloadFile()处 2015\Projects\unzip\unzip\Form1.cs:第30行 在c:\users\ethicpro\documents\visual studio 2015\Projects\unzip\unzip\Form1.cs中的unzip.Form1..ctor()处:第20行 在c:\users\ethicpro\documents\visual studio 2015\Projects\unzip\unzip\Program.cs中的unzip.Program.Main()处:第19行 位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args) 位于System.AppDomain.nExecuteAssembly(运行时程序集,字符串[]args) at System.Runtime.Hosting.ManifestRunner.Run(布尔checkAptModel) 在System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()上 在System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext)中 activationContext,字符串[]activationCustomData) 在System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext)中 激活上下文) 位于System.Activator.CreateInstance(ActivationContext ActivationContext) 位于Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()处 位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态) 位于System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback回调、对象状态、布尔值 (同步CTX) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔值 (同步CTX) 在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态) 位于System.Threading.ThreadHelper.ThreadStart()的内部异常:

链接是:

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

试试这个

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);
    }

假设服务器正确使用响应代码,这意味着不允许您使用响应代码。除非您没有提供正确的凭据,否则它可能与您的代码无关。正确的凭据包括正确的用户代理,如果它们阻止了类似scraper的代码。您还发现了哪些其他问题?您如何提供您的凭证?你能用你的浏览器下载这个文件吗?如果你添加一个合理的u/a:
wc.Headers.add(“用户代理”,“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/61.0.3163.31 Safari/537.36”)@AlexK。谢谢,它很有魅力。非常感谢你!!谢谢,这很有帮助。