Trouble with direct download links from Mediafire

1.8k Views Asked by At

I had trouble downloading a file from Mediafire. I found out the I have to use their API. I found another SO question: "Get direct download link and file site from Mediafire.com"

With the help of the shown functions I created the following class:

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"); 
    }

}

and used the function by T3KBAU5 like this:

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;
    }
}

But when I run it this error pops up: Screenshot of the Error

What can I do to solve the problem, and can you explain what this error means?

2

There are 2 best solutions below

0
tom982 On

Firstly, the Mediafiredownload method returns a string, the direct download link, which you are not using. Your code should resemble:

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");

As for the exception it's firing, it's important to understand what the GetNextIndexOf method is doing - iterating through a string, source, to find the index of a character, c, after a certain start position, start. The first line in that method is checking that the start value is within the length of the source string, so that it doesn't immediately look at a character out of the range and throw an ArgumentOutOfRangeException. You need to set a breakpoint on this line:

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

And look at the values of str and indexurl using the locals window. This will reveal the problem.

Also, the code you are using is almost 5 years old and I expect this problem is more to do with the fact that Mediafire will have changed the URL structure since then. Your code relies on the fact that the url contains "http://download" which may not be the case any more.

2
DragonSkills99 On

The easiest way is to use an dll file. Such as DirektDownloadLinkCatcher.

Or u have to query for the right div by the "download_link" class and the get the href of the containing tag. Thats the way how I solved it in these dll.^^

Or use the API from MediaFire.

Hoped I could help.