Получение Cookies по HttpWebRequest - C#

Узнай цену своей работы

Формулировка задачи:

Решил я написать программу которая обменивает пароль и логин на кукисы, но они почему то не приходят, хотя я полностью дублировал post запрос с снифера.
Листинг программы
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. using System.ComponentModel;
  10. using System.Collections.Generic;
  11. namespace FastDoorSaw
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public HttpWebRequest request;
  16. public HttpWebResponse response;
  17. public string SomeText_2;
  18. public byte[] byteArray;
  19. public Stream netStream;
  20. public string SomeText;
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. }
  25. private string GetCook(string getmail, string getpass, string gethost)
  26. {
  27. string remixcook = "fail =(";
  28. try
  29. {
  30. request = (HttpWebRequest)WebRequest.Create(new Uri("http://" + gethost));
  31. request.AllowAutoRedirect = false;
  32. request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; ru) Opera 8.50";
  33. request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
  34. request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
  35. request.Method = "GET";
  36. request.CookieContainer = new CookieContainer();
  37. response = (HttpWebResponse)request.GetResponse();
  38. SomeText = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("windows-1251")).ReadToEnd();
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. request = (HttpWebRequest)WebRequest.Create(new Uri("https://login." + gethost + ":443/?act=login"));
  41. SomeText_2 = "act=login&q=1&al_frame=1&expire=&captcha_sid=&captcha_key=&from_host=" + gethost + "&ip_h=" + SomeText.Substring(SomeText.IndexOf("ip_h: '") + 7, 18) + "&email=" + getmail + "&pass=" + getpass;
  42. request.Headers.Add("DNT", "1");
  43. request.Headers.Add("Accept-Encoding", "gzip, deflate");
  44. request.Headers.Add("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.7");
  45. request.Method = "POST";
  46. request.Referer = "http://" + gethost + "/al_index.php?act=auth_frame";
  47. byteArray = Encoding.UTF8.GetBytes(SomeText_2);
  48. request.ContentLength = byteArray.Length;
  49. Stream dataStream = request.GetRequestStream();
  50. dataStream.Write(byteArray, 0, byteArray.Length);
  51. dataStream.Close();
  52. request.AllowAutoRedirect = true;
  53. response = (HttpWebResponse)request.GetResponse();
  54. SomeText = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("windows-1251")).ReadToEnd();
  55. foreach (Cookie c in response.Cookies)
  56. {
  57. remixcook += "\r\n" + c.Name + " " + c.Value;
  58. }
  59. }
  60. catch (Exception lol)
  61. {
  62. MessageBox.Show(lol + "");
  63. }
  64. return remixcook;
  65. }
  66. private void button1_Click(object sender, EventArgs e)
  67. {
  68. MessageBox.Show(GetCook("моёмыло%40gmail.com", "пароль", "vk.com"));
  69. }
  70. }
  71. }
В ответ должны приходить куки "l=idчеловека" и "p=непонятныйхеш" Но самое главное это строка для пере адресации: "Location: http://vk.com/login.php?act=slogin&al_frame=1&hash=буковкиициферки&s=1" Попытался сделать запрос такого типа:
Листинг программы
  1. request = (HttpWebRequest)WebRequest.Create(new Uri("http://" + gethost + "/login.php?act=login&q=1&al_frame=1&expire=&captcha_sid=&captcha_key=&from_host=" + gethost + "&ip_h=" + SomeText.Substring(SomeText.IndexOf("ip_h: '") + 7, 18) + "&email=" + getmail + "&pass=" + getpass));
  2. request.Method = "POST"; //GET тоже побывал
  3. request.AllowAutoRedirect = true;
  4. response = (HttpWebResponse)request.GetResponse();
  5. remixcook += "\r\n" + response.GetResponseHeader("Location");
  6. foreach (Cookie c in response.Cookies)
  7. {
  8. remixcook += "\r\n" + c.Name + " " + c.Value;
  9. }
Всё равно не кукисов, не Location

Решение задачи: «Получение Cookies по HttpWebRequest»

textual
Листинг программы
  1. captcha_sid=&captcha_key=

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут