-1

我正在尝试使用 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 到 http 的 https,但没有成功。当我将这些 url 粘贴到 Chrome 或 FireFox 或 IE 中时,我可以下载文件,有时它会给出 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 行代码。

注意:相同的代码适用于其他网站,仅适用于本网站会出错。

4

4 回答 4

1

正如我在评论中提到的,这里的问题是服务器期望存在 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发送页码和其他参数。

于 2019-06-17T16:26:39.083 回答
0

我使用您的 URL 进行了测试,并且能够重现您的错误。我尝试使用 querystring 参数的任何请求quantity=0似乎都以HTTP Error 403.

我建议要求quantity大于零。

于 2019-06-17T15:12:41.567 回答
0

HTTP 403 状态代码表示禁止,因此您的凭据有问题。好像你没有发送任何东西。如果您将它们添加到您的标题中,这应该可以正常工作,如下所示:

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

或像这样发送它们:

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

链接很可能通过 Web 浏览器工作,因为您已经通过身份验证并且浏览器正在发送凭据/令牌。

于 2019-06-17T15:15:55.920 回答
-1

Digi-key 也有这个问题。

我的解决方案是关闭我的 VPN 服务。

于 2020-04-21T10:34:32.533 回答