问题
I'm looking for the way to get the direct link from mediafire. By default, when a user visits a download link, he will be presented with a download page where he has to wait for the download to be processed and then a link will appear.
I googled and found a VB.NET 2008 solution to this using WebBrowser WB
http://www.vbforums.com/showthread.php?t=556681
It works pretty well but I'm tired of pop-up windows and the loading speed. So, I wonder if there is a solution to this problem? (a non WB solution ^^)
Any help is greatly appreciated.
回答1:
- How to make a Get request using C#
- Beyond the basics (Handling cookies)
I'll be posting back when I've worked the regular expression into the code, not sure this is going to work though as I think the actual link is obtained through AJAX. I'm still playing with this.
AJAX concerns discussed: StackOverflow related question
Based on the php code provided in the comments:
- 1st Response->fetch the value passed to a function called "cG(var1,var2,var3)" I don't think mediafire still uses that function, it seems it's called "cu(var1,var2,var3)" now, not sure if the php you gave will still work. anyway, we can do the same thing get the values from the cu function & post our request to http://www.mediafire.com/dynamic/download.php? with the cookie we retrieved from our first response.
- The 2nd response creates this huge list of random generated variables, then generates the download url concatenating some of those variables, the only way to get the url out if this is by using the Microsoft.JScript engine to evaluate this code. I'll be posting my code asap
Code (warning this code is ugly & needs to be cleaned up):
string sURL = "http://www.mediafire.com/?syzjuytmdkn";
HttpWebRequest wrGETURL = (HttpWebRequest)WebRequest.Create(sURL);
wrGETURL.CookieContainer = new CookieContainer();
wrGETURL.Referer = "http://www.mediafire.com";
wrGETURL.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
HttpWebResponse wrResponse = (HttpWebResponse)wrGETURL.GetResponse();
CookieCollection cookies = wrResponse.Cookies;
Here we send the first request & Store the cookies received. Next we want to parse the page to find out the keys for the 2nd request:
StreamReader objReader = new StreamReader(wrResponse.GetResponseStream());
string[] parameters = {};//will contain the parameters fetched
string html = objReader.ReadToEnd();
int cupos1 = html.IndexOf("cu(");
int cupos2 = html.IndexOf("')",cupos1);
string[] separators = { "','"};
parameters = html.Substring(cupos1 + 4, cupos2 - cupos1 - 4)
.Split(separators, StringSplitOptions.None);
Fetch the 2nd page which will contain the encoded download url:
string sURL2 = String.Format("http://www.mediafire.com/dynamic/download.php?qk={0}&pk={1}&r={2}",
parameters[0],parameters[1],parameters[2]);
HttpWebRequest wrGETURL2 = (HttpWebRequest)WebRequest.Create(sURL2);
wrGETURL2.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
wrGETURL2.Referer = "http://www.mediafire.com";
wrGETURL2.CookieContainer = new CookieContainer();
wrGETURL2.CookieContainer.Add(cookies);
wrResponse = (HttpWebResponse)wrGETURL2.GetResponse();
objReader = new StreamReader(wrResponse.GetResponseStream());
html = objReader.ReadToEnd();
This html contains the Javascript that will generate the download url, here we extract it, then evaluate it & finaly write it to the console:
int varpos1 = html.IndexOf("<script language=\"Javascript\">")+35;
//The variables are declared just before the 'function'
int varpos2 = html.IndexOf("function",varpos1);
string vardata = html.Substring(varpos1, varpos2 - varpos1);
int hrefpos1 = html.IndexOf("href=\\\"http://", varpos2)+6 ;
int hrefpos2 = html.IndexOf(">", hrefpos1);
string hrefdata = String.Format("var url = {0};", html.Substring(hrefpos1, hrefpos2 - hrefpos1-5));
object Result = EvalJScript(vardata + "\n" + hrefdata);
Console.WriteLine(Result.ToString());
This stuff worked for me, but needs to be rewritten, I also leave the EvalJScript function for you to work as the one I'm using (from Evaluating JScript in c#) is deprecated
回答2:
Dim req As HttpWebRequest, res As HttpWebResponse
Dim cok As New CookieContainer, str As String, match As Match
req = WebRequest.Create("http://www.mediafire.com/?65d1dftjwml")
req.CookieContainer = cok
res = req.GetResponse
str = New StreamReader(res.GetResponseStream).ReadToEnd
match = Regex.Match(str, "cu\('(.+)','(.+)','(.+)'\);")
Dim qk As String = match.Groups(1).Value
Dim pk As String = match.Groups(2).Value
Dim r As String = match.Groups(3).Value
Dim t As String = "http://www.mediafire.com/dynamic/download.php?qk=" & qk & "&pk=" & pk & "&r=" & r & "&ukey=" & res.Cookies.Item("ukey").Value
req = WebRequest.Create(t)
res = req.GetResponse
txtcode.Text = New StreamReader(res.GetResponseStream).ReadToEnd
回答3:
the final block of code on my original answer is cut off - it shows the C# code on evaluating jscript:
int varpos1 = html.IndexOf("<script language=\"Javascript\">")+35;
//The variables are declared just before the 'function'
int varpos2 = html.IndexOf("function",varpos1);
string vardata = html.Substring(varpos1, varpos2 - varpos1);
int hrefpos1 = html.IndexOf("href=\\\"http://", varpos2)+6 ;
int hrefpos2 = html.IndexOf(">", hrefpos1);
string hrefdata = String.Format("var url = {0};", html.Substring(hrefpos1, hrefpos2 - hrefpos1-5));
object Result = EvalJScript(vardata + "\n" + hrefdata);
Console.WriteLine(Result.ToString());
来源:https://stackoverflow.com/questions/1175913/getting-mediafire-directlink-without-using-webbrowser-net-control