Не передаются cookies (secure?) авторизация на mail.ru - C#
Формулировка задачи:
Вот мой ужасный тестовый код. Проблема в том, что не все кукисы, которые mail.ru устанавливает, передаются. Те что secure,( не сохраняются в контейнере, я так понял, и) не передаются в следующих запросах. Проверял в fiddler2.
Буду благодарен за любую помощь!
public static void Main() { string url = "https://auth.mail.ru/cgi-bin/auth"; string Data = "Domain=mail.ru&Login=free_lsd&Password=YYY&new_auth_form=1&saveauth=1"; CookieContainer cookies = new CookieContainer(); string cook; //HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://mail.ru"); /* req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"; //req.Referer = "https://auth.mail.ru/cgi-bin/auth?from=splash"; //req.Method = "GET"; //req.CookieContainer = cookies; req.KeepAlive = true; //req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*//*;q=0.8"; req.Headers.Add("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4"); */ //HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"; req.Method = "POST"; req.Timeout = 10000; req.Referer = "https://mail.ru/?from=logout&ref=main"; req.ContentType = "application/x-www-form-urlencoded"; req.CookieContainer = cookies; req.KeepAlive = true; req.Headers.Add("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4"); req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; //req.Headers.Add("Accept-Encoding", "gzip, deflate, sdch"); byte[] SentData = Encoding.GetEncoding(1251).GetBytes(Data); req.ContentLength = SentData.Length; Stream SendStream = req.GetRequestStream(); SendStream.Write(SentData, 0, SentData.Length); SendStream.Close(); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream ReceiveStream = resp.GetResponseStream(); StreamReader sr = new StreamReader(ReceiveStream, Encoding.UTF8); char[] read = new char[256]; int count = sr.Read(read, 0, 256); string Out = String.Empty; while (count > 0) { String str = new String(read, 0, count); Out += str; count = sr.Read(read, 0, 256); } cook = string.Empty; foreach (Cookie c in resp.Cookies) { cook += c + "\r\n"; } Console.WriteLine(cook); //cookies = resp.CookieContainer; //Console.WriteLine(resp.Headers); StreamWriter OutFile = new StreamWriter("C:\\Users\\alex\\Desktop\\out.html"); OutFile.Write(Out); OutFile.Close(); //req = (HttpWebRequest)WebRequest.Create("https://auth.mail.ru/sdc?from=https%3A%2F%2Fe.mail.ru%2Fmessages%2Finbox%2F%3Fback%3D1"); HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create("https://e.mail.ru/messages/inbox/?back=1"); req2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"; req2.Referer = "https://auth.mail.ru/cgi-bin/auth?from=splash"; req2.Method = "GET"; req2.CookieContainer = cookies; Console.WriteLine(); //Console.Read(); req2.KeepAlive = true; req2.Headers.Add("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4"); req2.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; //req.Headers.Add("Accept-Encoding", "gzip, deflate, sdch"); //req.AllowAutoRedirect = false; //req.Headers.Add(resp.Headers["Set-Cookie"].ToString()); resp = (HttpWebResponse)req2.GetResponse(); sr = new StreamReader(resp.GetResponseStream()); Out = sr.ReadToEnd(); //Console.WriteLine(Out); OutFile = new StreamWriter("C:\\Users\\alex\\Desktop\\out.html"); OutFile.Write(Out); OutFile.Close(); cook = string.Empty; foreach (Cookie c in resp.Cookies) { cook += c + "\r\n"; } Console.WriteLine(cook); Console.WriteLine(); Console.Read(); /* string url = "http://mail.ru"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse )request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader sr = new StreamReader(stream); string Out = sr.ReadToEnd(); sr.Close(); //Console.WriteLine(Out); //Console.Read(); StreamWriter srOut = new StreamWriter("C:\\Users\\alex\\Desktop\\out.html"); srOut.Write(Out); srOut.Close(); return; */ }
Решение задачи: «Не передаются cookies (secure?) авторизация на mail.ru»
textual
Листинг программы
Location: [url]http://e.mail.ru/login?fail=1&sdc=1&page=https%3A%2F%2Fe.mail.ru%2Fmessages%2Finbox%2F%3Fback%3D1[/url]
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д