簡體   English   中英

WebClient:下載未完成:遠程服務器返回錯誤:(403)禁止。

[英]WebClient : Download Not Complete: The remote server returned an error: (403) Forbidden.

我正在使用此代碼下載文件

      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在Free download Manager中進行下載時

我加了這條線

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

但它仍然無法正常工作。

如果有人可以幫助我,將不勝感激。

提前致謝。

聽起來您正在使用的免費下載管理器可能欺騙了引薦標頭,而您的實現卻並非如此。 如果引用站點字段設置為特定值(即服務器上的站點),則服務器可能會將您嘗試下載的文件的下載限制為只能下載。 您是否嘗試過:

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瀏覽器的auseragent並查看是否可以下載文件。

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

您可以在互聯網上找到用戶代理字符串的完整列表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM