Массовое скачивание файлов - C#
Формулировка задачи:
Подскажите, как можно скачать торрент-файл с сайта по ссылке средствами с#? Есть вариант кода, который скачивает файлы. Но проблема в том, что при попытке открыть скачанный таким образом файл в торрент-клиенте выдает ошибку "torrent is not valid bencoding".Как можно решить данную проблему?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace Dovnloader
{
class Program
{
static void Main(string[] args)
{
WebClient wc = new WebClient();
string url;
string path;
string name;
for (int i = 0; i < 1000; i++)
{
url = "https://somesite/" + i + "/download";
path = "E:\\torrents";
name = i+".torrent";
wc.DownloadFile(url, path + name);
}
}
}
}Решение задачи: «Массовое скачивание файлов»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Threading;
using System.IO;
namespace Downloader
{
class Program
{
static void Main(string[] args)
{
var url = new StringBuilder("");
string Folderpath = @"C:\TestFrom";
var Name = new StringBuilder("");
var fullPath = new StringBuilder("");
int FileCounter = 0;
int StartIndex = 782225;
int StopIndex = 785235;
var LinksList = new List<(string, string)>(Math.Abs(StopIndex - StartIndex));
for (int i = StartIndex; i <= StopIndex; i++)
{
url.Clear();
Name.Clear();
fullPath.Clear();
url.AppendFormat($"http://tfile.me/forum/download.php?id={i.ToString()}&ak=1111111111");
Name.AppendFormat($"{i.ToString()}.torrent");
fullPath.AppendFormat($"{Folderpath}{Name}");
LinksList.Add((url.ToString(), fullPath.ToString()));
}
foreach (var link in LinksList)
{
try
{
WebClient wc = new WebClient();
wc.DownloadFile(new Uri(link.Item1), link.Item2);
// Console.WriteLine($"Downloaded: {link.Item1} to {link.Item2}");
FileCounter++;
}
catch (Exception exc)
{
Console.WriteLine(exc.Message + $" {link.Item1}");
}
}
Console.WriteLine($"{FileCounter} files downloaded");
Console.WriteLine("End of the program");
Console.ReadKey(true);
}
}
}