[英]WebClient 403 Forbidden
我可以在IE中手動下載。
但是,使用以下代碼
WebClient client = new WebClient();
client.DownloadFile(address, filename);
顯示例外:403禁止
怎么了? 我怎樣才能做到這一點?
其他
只需在下載之前添加一行簡單的行:
string url = ...
string fileName = ...
WebClient wb = new WebClient();
wb.Headers.Add("User-Agent: Other"); //that is the simple line!
wb.DownloadFile(url, fileName);
而已。
403也可能由TLS問題引起。 要進行驗證,您應該檢查WebException.Response對象的文本。
catch (WebException ex)
{
if (ex.Response != null)
{
var response = ex.Response;
var dataStream = response.GetResponseStream();
var reader = new StreamReader(dataStream);
var details = reader.ReadToEnd();
}
}
如果是TLS,請嘗試將其添加到您的代碼中以強制使用TLS1.2。
對於.net4:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
對於.net4.5或更高版本:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
嘗試從SharePoint網站URL下載圖像時遇到此問題。 在我的情況下,將user-agent
設置為其他或標題中的空白是不夠的,我必須設置user-agent
,如下所示:
client.Headers.Add("user-agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
該解決方案來自這個答案 。
我在IE中得到403,我想你需要登錄才能檢索資源。 您的瀏覽器可能已緩存憑據,但您的應用並非旨在讓您登錄。或者您是否在瀏覽器中登錄Google - 嘗試退出並查看您是否仍然可以訪問....
在調用DownloadFile
方法之前,需要設置適當的http標頭。
WebClient webClient = new WebClient();
webClient.Headers.Add("???", "???");
webClient.Headers.Add("???", "???");
webClient.Headers.Add("???", "???");
webClient.DownloadFile(address, filename);
放置正確的值而不是這些問號可能會很棘手。 您需要下載Fiddler或其他程序或webbrowser擴展程序,以顯示您的webbrowser向Google發送的HTTP標頭,並基本上在您的程序中復制相同的請求。
這就是我發生的事情:
我試圖下載一個(公共).xls文件(通過DownloadFile方法),該文件可以從所有瀏覽器中輕松下載。
在嘗試並努力解決所有答案(但沒有運氣)之后,我終於打開了堆棧並注意到一些奇怪的東西(參見截圖)。
雖然,該文件是通過瀏覽器中的http下載的,但它通過DownloadFile方法給出403錯誤。
最后,我剛剛將URL從http:// something更改為https:// something ,並且工作正常。
希望這可以幫助!
解決這個問題的關鍵是通過代碼執行一次請求,第二次在瀏覽器中,使用Fiddler記錄兩個請求並確保標頭匹配。
我最終不得不為以下內容添加標頭:
我希望這有助於未來的人們。
我在嘗試下載Amazon 3S網址上的文件時遇到了同樣的問題。 我在這里寫了博客: http : //blog.cdeutsch.com/2010/11/net-webclient-403-forbidden-error.html
我在這里找到了最終的解決方案: 使用url編碼的斜杠獲取URL
我遇到了類似的問題,試圖從幾個特定的網站下載文件,導致某些文件返回403錯誤,但其他文件沒有。
我已經嘗試過User-Agent標頭,接受標頭,嘗試https網址和各種其他設置,但仍然沒有成功。
這兩個URL都將在瀏覽器中加載,並且不需要在網站上進行任何身份驗證即可訪問它們(它們是公共訪問),但是一個將下載而另一個將返回403。
對原因是什么以及如何解決的任何幫助。
static void Main(string[] args)
{
WebClient webClient = new WebClient();
webClient.Headers.Add("Accept: text/html, application/xhtml+xml, application/pdf, */*");
webClient.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
webClient.Headers.Add("Accept-Encoding: gzip, deflate, br");
webClient.Headers.Add("Accept-Language: en-US,en;q=0.9");
webClient.Headers.Add("Cache-Control: no-cache");
webClient.Headers.Add("Upgrade-Insecure-Requests: 1");
try
{
webClient.DownloadFile(new Uri("https://www.vigil.aero/wp-content/uploads/PSB-10-2013-06-14-.pdf"), "test1.pdf");
Console.WriteLine("Complete");
}
catch (Exception ex)
{
Console.WriteLine("{0}", ex.Message);
}
try
{
webClient.DownloadFile(new Uri("https://www.vigil.aero/wp-content/uploads/PSB-9-2013-06-14.pdf"), "test2.pdf");
Console.WriteLine("Complete");
}
catch (Exception ex)
{
Console.WriteLine("{0}", ex.Message);
}
Console.ReadLine();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.