来自Mediafire的直接下载链接有问题

时间:2015-11-10 18:10:07

标签: c# download mediafire

我无法从Mediafire下载文件。我发现我必须使用他们的API。我发现了另一个问题:"Get direct download link and file site from Mediafire.com"

在所示功能的帮助下,我创建了以下类:

class Program
{
    static void Main(string[] args)
    {
        Mediafireclass mf = new Mediafireclass();
        WebClient webClient = new WebClient();

        mf.Mediafiredownload("somemediafirelink/test.txt");
        webClient.DownloadFileAsync(new Uri("somemediafirelink/test.txt"), @"location to save/test.txt"); 
    }

}

并使用T3KBAU5的功能:

internal class Mediafireclass
{
    public string Mediafiredownload(string download)
    {
        HttpWebRequest req;
        HttpWebResponse res;
        string str = "";
        req = (HttpWebRequest)WebRequest.Create(download);
        res = (HttpWebResponse)req.GetResponse();
        str = new StreamReader(res.GetResponseStream()).ReadToEnd();
        int indexurl = str.IndexOf("http://download");
        int indexend = GetNextIndexOf('"', str, indexurl);
        string direct = str.Substring(indexurl, indexend - indexurl);
        return direct;
    }

    private int GetNextIndexOf(char c, string source, int start)
    {
        if (start < 0 || start > source.Length - 1)
        {
            throw new ArgumentOutOfRangeException();
        }
        for (int i = start; i < source.Length; i++)
        {
            if (source[i] == c)
            {
                return i;
            }
        }
        return -1;
    }
}

但是当我运行它时会弹出这个错误: Screenshot of the Error

我该怎么做才能解决问题,你能解释一下这个错误意味着什么吗?

2 个答案:

答案 0 :(得分:0)

首先,Mediafiredownload方法返回一个字符串,即您未使用的直接下载链接。您的代码应该类似于:

Mediafireclass mf = new Mediafireclass();
WebClient webClient = new WebClient();

string directLink = mf.Mediafiredownload("somemediafirelink/test.txt");
webClient.DownloadFileAsync(new Uri(directLink), @"location to save/test.txt");

至于它的触发异常,理解GetNextIndexOf方法正在做什么很重要 - 在一定的开始之后迭代字符串,源,找到字符的索引c位置,开始。该方法的第一行是检查起始值是否在源字符串的长度内,这样它就不会立即查看该范围之外的字符并抛出ArgumentOutOfRangeException。您需要在此行上设置断点:

int indexend = GetNextIndexOf('"', str, indexurl);

使用locals窗口查看str和indexurl的值。这将揭示问题。

此外,您使用的代码已有近5年的历史,我预计此问题更多地与Mediafire此后已更改URL结构的事实有关。您的代码依赖于网址包含&#34; http://download&#34;可能不再是这种情况了。

答案 1 :(得分:0)

最简单的方法是使用dll文件。例如DirektDownloadLinkCatcher

或者你必须通过“download_link”类查询正确的div并获取包含标记的href。这就是我在这些dll中解决它的方式。^^

或使用MediaFire中的API。

希望我能提供帮助。

相关问题