WebClient: Download Not Complete:远程服务器返回一个错误:(403)Forbidden

本文关键字:错误 Forbidden 一个 返回 Not Download Complete 服务器 WebClient | 更新日期: 2023-09-27 18:15:10

我正在使用此代码下载文件

      private WebClient client;    
        client = new WebClient();      
        if (isBusy)
        {
            client.CancelAsync();
            isBusy = false;
            this.downloadButton.Text = "Download";
        }
        else
        {
            try {
                Uri uri = new Uri(urlTextBox.Text);
                this.downloadProgressBar.Value = 0;
                client.Headers.Add("User-Agent: Other");
                client.DownloadFileAsync(uri, "test.csv.zip");         
                this.downloadButton.Text = "Cancel";
                isBusy = true;
            }
            catch (UriFormatException ex) {
                MessageBox.Show(ex.Message);
            }
        }

但是我得到了一个错误错误是

  Download Not Complete: The remote server returned an error: (403) Forbidden.

我不知道它为什么会来。

但是当我在免费下载管理器中使用uri下载时,其工作

我添加了这行

             client.Headers.Add("User-Agent: Other");

但是它仍然不能工作。

如果有人能帮助我,我将不胜感激。

WebClient: Download Not Complete:远程服务器返回一个错误:(403)Forbidden

听起来你正在使用的免费下载管理器可能会欺骗引用头,而你的实现不是。服务器可能会限制你试图下载的文件的下载,只有当referer字段设置为特定值(即服务器上的站点)时才可以下载。

client.Headers.Add("referer", uri);

可能值得使用Fiddler来查看下载管理器发送的请求与您的请求之间的差异,然后修改您的请求直到它正常工作。

编辑:

我已经测试了您提供的URL,并通过添加以下内容使其在本地工作:

client.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
client.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

你需要提供一个"Accept"头,否则服务器不知道你的客户端想要/将接受什么。下面是我完整的匿名样例应用程序(为了简单起见,使用Sleep()):

        string url = "http://..."; // Change this to the full url of the file you want to download 
        string filename = "downloadedfile.zip"; // Change this to the filename you want to save it as locally.
        WebClient client = new WebClient(); 
        try 
        {
            Uri uri = new Uri(url);
            client.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
            client.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
            client.DownloadFileAsync(uri, filename);
            while (client.IsBusy)
            {
                System.Threading.Thread.Sleep(1000);
            }
        }
        catch (UriFormatException ex) 
        {
            Console.WriteLine(ex.Message);
        }

如果用户没有查看/下载特定内容的权限,通常会返回403 Forbidden错误。

你提到它在免费下载管理器中工作,但你没有提到你是否在免费下载管理器中提供了认证信息(是的,你可以这样做)。

无论如何你的用户代理也可能是一个问题,一些网站不允许客户端与未知的用户代理,尝试添加一个流行的web浏览器的用户代理,看看你是否可以下载文件

IE 10.6 Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0

尝试将上述IE 10.6用户代理添加到您的应用程序

 client.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0");

你可以在互联网上找到一个完整的useragent字符串列表