Je souhaite télécharger un fichier .torrent à partir d'une distribution Linux, mais pour une raison quelconque, le dernier fichier téléchargé depuis mon application est différent de celui téléchargé manuellement. Celui que mon application télécharge a 31KB et c'est un fichier .torrent invalide, alors que le droit (quand je le télécharge manuellement) a 41KB et il est valide.WebClient ne télécharge pas le bon fichier à partir de l'URL fournie
L'URL du fichier que je veux télécharger est http://torcache.net/torrent/C348CBCA08288AE07A97DD641C5D09EE25299FAC.torrent
Pourquoi cela se produit et comment puis-je télécharger le même fichier (le valide, avec 41Ko)?
Merci.
C# Code de la méthode qui télécharge le fichier ci-dessus:
string sLinkTorCache = @"http://torcache.net/torrent/C348CBCA08288AE07A97DD641C5D09EE25299FAC.torrent";
using (System.Net.WebClient wc = new System.Net.WebClient())
{
var path = @"D:\Baixar automaticamente"; // HACK Pegar isso dos settings na versão final
var data = Helper.Retry(() => wc.DownloadData(sLinkTorCache), TimeSpan.FromSeconds(3), 5);
string fileName = null;
// Try to extract the filename from the Content-Disposition header
if (!string.IsNullOrEmpty(wc.ResponseHeaders["Content-Disposition"]))
{
fileName = wc.ResponseHeaders["Content-Disposition"].Substring(wc.ResponseHeaders["Content-Disposition"].IndexOf("filename=") + 10).Replace("\"", "");
}
var torrentPath = Path.Combine(path, fileName ?? "Arch Linux Distro");
if (File.Exists(torrentPath))
{
File.Delete(torrentPath);
}
Helper.Retry(() => wc.DownloadFile(new Uri(sLinkTorCache), torrentPath), TimeSpan.FromSeconds(3), 5);
}
Helper.Retry (Essayez d'exécuter la méthode à nouveau en cas d'exceptions HTTP):
public static void Retry(Action action, TimeSpan retryInterval, int retryCount = 3)
{
Retry<object>(() =>
{
action();
return null;
}, retryInterval, retryCount);
}
public static T Retry<T>(Func<T> action, TimeSpan retryInterval, int retryCount = 3)
{
var exceptions = new List<Exception>();
for (int retry = 0; retry < retryCount; retry++)
{
try
{
if (retry > 0)
System.Threading.Thread.Sleep(retryInterval); // TODO adicionar o Using pro thread
return action();
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
throw new AggregateException(exceptions);
}
Pourquoi télécharger le fichier que vous deux fois dans votre code? 'DownloadData' et' DownloadFile' – Rob