网络客户端未从提供的 URL 下载正确的文件



我想从 Linux 发行版下载一个.torrent文件,但由于某种原因,从我的应用程序下载的最终文件与手动下载的文件不同。我的应用程序下载的那个有 31KB,它是一个无效的.torrent文件,而右边的一个(当我手动下载时)有 41KB 并且有效。

我要下载的文件的 URL 是 http://torcache.net/torrent/C348CBCA08288AE07A97DD641C5D09EE25299FAC.torrent

为什么会发生这种情况以及如何下载相同的文件(有效文件,41KB)?

谢谢。


下载上述文件的方法中的 C# 代码:

        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(在出现 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);
    }

我最初认为该网站会用垃圾响应,如果它认为这是来自机器人的请求(也就是说,它正在检查一些标头)。在与Fiddler一起查看后 - 似乎返回的数据对于Web浏览器和代码完全相同。这意味着,我们没有正确地放气(提取)响应。Web服务器压缩数据(使用gzip之类的东西)是很常见的。 WebClient不会自动压缩数据。

使用通过WebClient.DownloadData自动解压缩gzip响应的答案 - 我设法让它正常工作。

另请注意,您要下载文件两次。你不需要这样做。

工作代码:

//Taken from above linked question
class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        return request;
    }
}

并使用它:

string sLinkTorCache = @"http://torcache.net/torrent/C348CBCA08288AE07A97DD641C5D09EE25299FAC.torrent";
using (var wc = new MyWebClient())
{
  var path = @"C:Junk";
  var data = Helper.Retry(() => wc.DownloadData(sLinkTorCache), TimeSpan.FromSeconds(3), 5);
  string fileName = "";
  var torrentPath = Path.Combine(path, fileName ?? "Arch Linux Distro.torrent");
  if (File.Exists(torrentPath))
      File.Delete(torrentPath);
    File.WriteAllBytes(torrentPath, data);
}

最新更新