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.
User contributions licensed under CC BY-SA 3.0