Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. //Отправляем запрос на первое получение куков для дальнейшей авторизации
  2. byte[] buffer = Encoding.ASCII.GetBytes("login_username=MYLOGIN&login_password=MYPASSWORD&login=%C2%F5%EE%E4");
  3. HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://sssr-rutracker.org/forum/login.php");//Строка для Post-запроса
  4. var cc = new CookieContainer();
  5. WebReq.CookieContainer = cc;//включаем куки
  6. WebReq.Method = "POST";
  7. WebReq.ContentType = "application/x-www-form-urlencoded";
  8. WebReq.ContentLength = buffer.Length;
  9. HttpWebResponse WebResp;
  10.  
  11. try
  12. {
  13. Stream PostData = WebReq.GetRequestStream();
  14. PostData.Write(buffer, 0, buffer.Length);
  15. PostData.Close();
  16. WebResp = (HttpWebResponse)WebReq.GetResponse();
  17.  
  18. }
  19. catch
  20. {
  21. MessageBox.Show("Ошибка сети", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
  22. return;
  23. }
  24.  
  25. //Отправляем запрос к странице с торрентом для получения ее содержимого
  26. var url = URL.Text;
  27. WebReq = (HttpWebRequest)WebRequest.Create(url);
  28. WebReq.CookieContainer = cc;
  29. WebReq.Method = "GET";
  30. WebReq.ContentType = "application/x-www-form-urlencoded";
  31. WebResp = (HttpWebResponse)WebReq.GetResponse();
  32.  
  33. string result;
  34. Encoding responseEncoding = Encoding.GetEncoding(WebResp.CharacterSet);
  35. try
  36. {
  37. using (StreamReader sr = new StreamReader(WebResp.GetResponseStream(), responseEncoding))
  38. {
  39. result = sr.ReadToEnd();
  40. }
  41. }
  42. catch
  43. {
  44. MessageBox.Show("Ошибка сети", "Заголовок сообщения", MessageBoxButtons.OK, MessageBoxIcon.Error);
  45. return;
  46. }
  47. //Скачать торрент
  48.  
  49. HtmlAgilityPack.HtmlDocument HD = new HtmlAgilityPack.HtmlDocument();
  50. HD.LoadHtml(result);
  51. var match = HD.DocumentNode.SelectSingleNode("//a[@class='dl-stub dl-link']").GetAttributeValue("href","");
  52. WebReq = (HttpWebRequest)WebRequest.Create("http://sssr-rutracker.org/forum/" + match.ToString());
  53. WebReq.CookieContainer = cc;
  54. WebReq.AllowAutoRedirect = false;
  55. WebReq.Method = "POST";
  56. WebReq.Referer = url;
  57. WebReq.ContentType = "application/x-www-form-urlencoded";
  58.  
  59. /*Пишем его в файл*/
  60. Stream ReceiveStream = WebReq.GetResponse().GetResponseStream();
  61. string filename = @"D:123.torrent";
  62. byte[] buffer1 = new byte[1024];
  63. FileStream outFile = new FileStream(filename, FileMode.Create);
  64. int bytesRead;
  65. while ((bytesRead = ReceiveStream.Read(buffer1, 0, buffer.Length)) != 0)
  66. outFile.Write(buffer1, 0, bytesRead);
  67. outFile.Close();
  68. MessageBox.Show("Файл загружен!");
  69. ReceiveStream.Close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement