-1

I am trying to download file from these links by using C# WebClient, but I am getting 403 error.

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

I tried to use different user agents, accept encoding etc. I replaced and tried https to http from url, but no success. When I paste these urls in Chrome or FireFox or IE, I am able to download file, sometimes it give 403 error, then I replace https to http from url, it downloads. But no success in webclient Tried Fiddler to inspect, no success Can someone try in your system, solve this problem.

Here is my code:

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

I know there are many threads related to this topic, I tried all of them, no success, please don't mark duplicate. Try in your system, this <10 lines of code.

Note: the same code is working for other websites, only for this website it is giving error.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Arun Banakar
  • 244
  • 1
  • 7
  • 4
    403 means authorization failed. Should you send credentials? – Jesse de Wit Jun 17 '19 at 15:10
  • Yeah Jesse de Wit, I followed this thread https://stackoverflow.com/a/8021144/4414852 but no success. – Arun Banakar Jun 17 '19 at 15:16
  • I think this is an issue with not having the right cookies. If I copy one of the links provided into a private browser window I get the 403 error. If I try to refresh the page it will work. Looking in the debug console I can see that after the 403 error I have a session cookie. If I delete this cookie I get the 403 error again. Somehow you need to get a session cookie. – amura.cxg Jun 17 '19 at 15:18
  • Hi amura cxg can you please show me how can I get session cookie. – Arun Banakar Jun 17 '19 at 15:27

4 Answers4

1

As I mentioned in my comment the issue here is that the server is expecting a cookie (specifically 'i10c.bdddb') to be present and is giving a 403 error when it's not. However, the cookie is sent with the 403 response. So you can make an initial junk request that will fail but give you the cookie. After this you can then proceed as normal.

Through some trial and error I was able to get the CSV using the code below:

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

Notes

Even with the right cookie if you don't send a User-Agent header the server will return a 403. I'm not sure what the server expects in terms of a user agent, I just copied the value my browser sends.

In the check to see if cookies have been set it would be a good idea to verify you actually have the 'i10c.bdddb' cookie instead of just checking if there are any cookies.

This is just a quick bit of sample code so it's not the cleanest. You may want to look into FormUrlEncodedContent to send the page number and other parameters.

amura.cxg
  • 2,348
  • 3
  • 21
  • 44
0

I tested with your URL and I was able to reproduce your error. Any requests that I try with the querystring parameter quantity=0 seems to fail with a HTTP Error 403.

I would suggest requesting a quantity greater than zero.

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
  • I don't think that matters here, I tried to remove other querystring parameter https://www.digikey.com/product-search/download.csv?FV=ffe00035&page=4&pageSize=500 but no use, I am able to download in Chrome and Firefox – Arun Banakar Jun 17 '19 at 15:19
  • how many records are you expecting if you request quantity 0? Is that a signal to get all items? I will try with other browsers. – Glenn Ferrie Jun 17 '19 at 15:51
  • please try this https://www.digikey.com/product-search/download.csv?FV=ffe00035&page=4&pageSize=500 this url doesn't have quantity, but still downloads csv – Arun Banakar Jun 17 '19 at 16:21
  • great. so then the `quantity=0` was for sure causing you a problem. *upvote* ;-) – Glenn Ferrie Jun 17 '19 at 16:56
0

A HTTP 403 status code mean forbidden, so there is a problem with your credentials. It doesn't seem to be like you're sending any. If you add them into your header this should work fine like this:

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

or sending them like this:

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

Most likely the links are working through web browsers is because you have already authenticated and the browser is sending the credentials/token.

Tom Dee
  • 2,516
  • 4
  • 17
  • 25
  • No Tom Dee. This website doesn't have credentials. Please try in your browser. – Arun Banakar Jun 17 '19 at 15:22
  • @ArunBanakar it is failed due to authorization hence the 403. If you don't actually authorise yourself when you downloaded the file in the browser, then the server must have generated a session token for you anyway. Most likely to prevent people from directly downloading the file. – Tom Dee Jun 17 '19 at 15:30
  • So how can I fix by session token... Tom Dee – Arun Banakar Jun 17 '19 at 16:24
-1

I have this issue with Digi-key too.

The solution for me is to turn off my VPN service.

Guest
  • 1