Can't download multiple pdfs in c#

0

Recently I tried downloading multiple pdf files in C# for a project I am working on. here's my code snippet:

private static void GetPdf()
{
    for (int i = 0; i <2; i++)
    {
        using (var client = new WebClient())
        {
            client.Headers.Add("User-Agent: Other");
            if (i == 0)
            {
                client.DownloadFile("https://files.geva.co.il/geva_website/uploads/2020/06/36371-2020.pdf", "ElectricityQ.pdf");
            }
            else
            {

                client.DownloadFile("https://files.geva.co.il/geva_website/uploads/2020/06/36371.pdf", "ElectricityA.pdf");
            }
        }
    }
}

static void Main(string[] args)
{
    GetPdf();
}

The problem is that after I download the first which works fine, when I try to download the second file I get this error.

System.Net.WebException
HResult=0x80131509
Message=The remote server returned an error: (403) Forbidden.
Source=System
StackTrace:
    at System.Net.WebClient.DownloadFile(Uri address, String fileName)
    at System.Net.WebClient.DownloadFile(String address, String fileName)
    at Server.Program.GetPdf() in C:\Users\m\source\repos\Test\Test\Program.cs:line 32
    at Server.Program.Main(String[] args) in C:\Users\m\source\repos\Test\Test\Program.cs:line 41

When I sent the code to my friends it worked for some of them without changing the code. It doesn't matter what file I chose. If I change the order of the download, the new first file will download but the second wont. No matter how many times I try, the second file doesn't download.

c#
http
webclient
downloadfile
asked on Stack Overflow Oct 20, 2020 by DataLord • edited Oct 20, 2020 by DataLord

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0