簡體   English   中英

WebClient 僅針對該網站返回 403 錯誤?

[英]WebClient returning 403 error only for this website?

我正在嘗試使用 C# WebClient 從這些鏈接下載文件,但出現 403 錯誤。

https://www.digikey.com/product-search/download.csv?FV=ffe00035&quantity=0&ColumnSort=0&page=5&pageSize=500

https://www.digikey.com/product-search/download.csv?FV=ffe00035&quantity=0&ColumnSort=0&page=4&pageSize=500

我嘗試使用不同的用戶代理,接受編碼等。我從 url 替換並嘗試了 https 到 http,但沒有成功。 當我在 Chrome、FireFox 或 IE 中粘貼這些 url 時,我能夠下載文件,有時會出現 403 錯誤,然后我將 https 從 url 替換為 http,它會下載。 但是webclient沒有成功嘗試Fiddler檢查,沒有成功有人可以在你的系統中嘗試,解決這個問題。

這是我的代碼:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
WebClient client= new WebClient();
Uri request_url = new Uri("https://www.digikey.com/product-search/download.csv?FV=ffe00035&quantity=0&ColumnSort=0&page=5&pageSize=500);
//tried http also http://www.digikey.com/product-search/download.csv?FV=ffe00035&quantity=0&ColumnSort=0&page=5&pageSize=500
client.Headers.Add("user-agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
client.DownloadFile(request_url, @"E:\123.csv");

我知道有很多線程與此主題相關,我嘗試了所有線程,但都沒有成功,請不要標記重復。 在您的系統中嘗試,這 <10 行代碼。

注意:相同的代碼適用於其他網站,僅適用於本網站時出錯。

正如我在評論中提到的,這里的問題是服務器期望存在 cookie(特別是“i10c.bdddb”),但如果不存在則給出 403 錯誤。 但是,cookie 與 403 響應一起發送。 因此,您可以發出一個初始垃圾請求,該請求將失敗但會為您提供 cookie。 在此之后,您可以照常進行。

通過一些試驗和錯誤,我能夠使用以下代碼獲取 CSV:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

CookieContainer cookieContainer = new CookieContainer();
Uri baseUri = new Uri("https://www.digikey.com");

using (HttpClientHandler handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (HttpClient client = new HttpClient(handler) { BaseAddress =  baseUri})
{
    //The User-Agent is required (what values work would need to be tested)
    client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0");

    //Make our initial junk request that will fail but get the cookie
    HttpResponseMessage getCookiesResponse = await client.GetAsync("/product-search/download.csv");

    //Check if we actually got cookies
    if (cookieContainer.GetCookies(baseUri).Count > 0)
    {
        //Try getting the data
        HttpResponseMessage dataResponse = await client.GetAsync("product-search/download.csv?FV=ffe00035&quantity=0&ColumnSort=0&page=4&pageSize=500");

        if(dataResponse.StatusCode == HttpStatusCode.OK)
        {
            Console.Write(await dataResponse.Content.ReadAsStringAsync());
        }
    }
    else
    {
        throw new Exception("Failed to get cookies!");
    }
}

筆記

即使使用正確的 cookie,如果您不發送User-Agent標頭,服務器也會返回 403。我不確定服務器對用戶代理的期望,我只是復制了瀏覽器發送的值。

在檢查是否已設置 cookie 時,最好驗證您是否確實擁有“i10c.bdddb”cookie,而不僅僅是檢查是否有任何 cookie。

這只是一小段示例代碼,所以它不是最干凈的。 您可能需要查看FormUrlEncodedContent以發送頁碼和其他參數。

我使用您的 URL 進行了測試,並且能夠重現您的錯誤。 我嘗試使用查詢字符串參數quantity=0任何請求似乎都失敗並顯示HTTP Error 403

我建議要求quantity大於零。

HTTP 403 狀態代碼表示禁止,因此您的憑據存在問題。 似乎您沒有發送任何內容。 如果您將它們添加到您的標題中,這應該可以正常工作:

client.Headers.Add("Authorization", "token");

或像這樣發送它們:

 client.UseDefaultCredentials = true;
 client.Credentials = new NetworkCredential("username", "password");

鏈接通過 Web 瀏覽器工作很可能是因為您已經進行了身份驗證並且瀏覽器正在發送憑據/令牌。

我的 Digi-key 也有這個問題。

我的解決方案是關閉我的 VPN 服務。

暫無
暫無

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

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