Penking Penking

Penking Penking

  • NA
  • 1
  • 2.6k

[C# - Winform]HttpWebRequest - Get direct link

Nov 10 2014 5:46 AM
I want to get free direct link download from this link.
 
Video:
https://www.youtube.com/watch?v=cS1anAUWvk4
 
 My code:
 
try { string Link = "http://rockfile.eu/829q5am73qz0.html"; CookieContainer Cook = new CookieContainer(); // --------------------------------------------------------------- STEP 1 => Access link to get post data for step2 string Link1 = Link; HttpWebRequest Request1 = (HttpWebRequest)WebRequest.Create(Link1); Request1.Method = "GET"; Request1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; Request1.CookieContainer = Cook; Request1.Host = "rockfile.eu"; Request1.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36"; Request1.KeepAlive = true; Request1.Timeout = 60000; HttpWebResponse Response1 = (HttpWebResponse)Request1.GetResponse(); StreamReader Reader1 = new StreamReader(Response1.GetResponseStream()); string HtmlSource1 = Reader1.ReadToEnd(); Reader1.Close(); Response1.Close(); Response1 = null; Request1.Abort(); HtmlAgilityPack.HtmlDocument HD1 = new HtmlAgilityPack.HtmlDocument(); // Using HtmlAgilityPack to extract data         HD1.LoadHtml(HtmlSource1); // ---------------------------------------------------------------------------- STEP 2 => Send free download request with post data extracted from step 1 // Extract post data from step 1 string op2 = HD1.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='op' and @value='download1']").Attributes["value"].Value; string usr_login2 = HD1.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='usr_login']").Attributes["value"].Value; string id2 = HD1.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='id']").Attributes["value"].Value; string fname2 = HD1.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='fname']").Attributes["value"].Value; string referer2 = HttpUtility.UrlEncode(HD1.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='referer']").Attributes["value"].Value); string method_free2 = HD1.DocumentNode.SelectSingleNode("//input[@type='submit' and @name='method_free' and @style='min-width:200px; height:39px; color:#fff;']").Attributes["value"].Value; string Link2 = Link; HttpWebRequest Request2 = (HttpWebRequest)WebRequest.Create(Link2); Request2.Method = "POST"; Request2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; Cook.Add(new Uri(Link2), new Cookie("_gat", "1")); // Add additional cookie Request2.CookieContainer = Cook; Request2.Host = "rockfile.eu"; Request2.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36"; Request2.KeepAlive = true; Request2.Timeout = 60000; Request2.Headers.Add("Origin", "http://rockfile.eu"); Request2.ContentType = "application/x-www-form-urlencoded"; Request2.ServicePoint.Expect100Continue = false; string PostData2 = Regex.Replace("op=" + op2 + "&usr_login=" + usr_login2 + "&id=" + id2 + "&fname=" + fname2 + "&referer=" + referer2 + "&method_free=" + method_free2,"\\s+","+"); byte[] ByteArray2 = Encoding.ASCII.GetBytes(PostData2); Request2.ContentLength = ByteArray2.Length; Stream Sr2 = Request2.GetRequestStream(); Sr2.Write(ByteArray2, 0, ByteArray2.Length); Sr2.Close(); HttpWebResponse Response2 = (HttpWebResponse)Request2.GetResponse(); StreamReader Reader2 = new StreamReader(Response2.GetResponseStream()); string HtmlSource2 = Reader2.ReadToEnd(); Reader2.Close(); Response2.Close(); Response2 = null; Request2.Abort(); HtmlAgilityPack.HtmlDocument HD2 = new HtmlAgilityPack.HtmlDocument();         HD2.LoadHtml(HtmlSource2); // ---------------------------------------------------------------------------- Step 3 => request direct link // Extract post data from step 2 string token3 = HD2.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='token']").Attributes["value"].Value; string op3 = HD2.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='op' and @value='download2']").Attributes["value"].Value; string id3 = HD2.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='id']").Attributes["value"].Value; string rand3 = HD2.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='rand']").Attributes["value"].Value; string referer3 = HttpUtility.UrlEncode(HD2.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='referer']").Attributes["value"].Value); string method_free3 = HD2.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='method_free']").Attributes["value"].Value; string method_premium3 = HD2.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='method_premium']").Attributes["value"].Value; string code3 = HttpUtility.HtmlDecode(HD2.DocumentNode.SelectSingleNode("//table//div").InnerText); string down_direct3 = HD2.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='down_direct']").Attributes["value"].Value; string Link3 = Link; HttpWebRequest Request3 = (HttpWebRequest)WebRequest.Create(Link3); Request3.Method = "POST"; Request3.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; Cook.Add(new Uri(Link3), new Cookie("_gali", "btn_download")); // Add additional cookie Request3.CookieContainer = Cook; Request3.Host = "rockfile.eu"; Request3.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36"; Request3.KeepAlive = true; Request3.Timeout = 60000; Request3.Headers.Add("Origin", "http://rockfile.eu"); Request3.ContentType = "application/x-www-form-urlencoded"; Request3.ServicePoint.Expect100Continue = false; string PostData3 = Regex.Replace("token=" + token3 + "&op=" + op3 + "&id=" + id3 + "&rand=" + rand3 + "&referer=" + referer3 + "&method_free=" + method_free3 + "&method_premium=" + method_premium3 + "&code=" + code3 + "&down_direct=" + down_direct3,"\\s+","+"); byte[] ByteArray3 = Encoding.ASCII.GetBytes(PostData3); Request3.ContentLength = ByteArray3.Length; Stream Sr3 = Request3.GetRequestStream(); Sr3.Write(ByteArray3, 0, ByteArray3.Length); Sr3.Close(); HttpWebResponse Response3 = (HttpWebResponse)Request3.GetResponse(); StreamReader Reader3 = new StreamReader(Response3.GetResponseStream()); string HtmlSource3 = Reader3.ReadToEnd(); Reader3.Close(); Response3.Close(); Response3 = null; Request3.Abort(); HtmlAgilityPack.HtmlDocument HD3 = new HtmlAgilityPack.HtmlDocument();         HD3.LoadHtml(HtmlSource3); // Extract direct link from HtmlSource3 string DirectLink = HD3.DocumentNode.SelectSingleNode("//a[@id='btn_downloadLink']").Attributes["href"].Value; // But this dont work // HtmlSource3 is the same with HtmlSource2 } } catch (Exception Ex) { MessageBox.Show(Ex.Message); }
But failed in request3, anobody help me fix this code.
 
Thanks