здраствуйте пытаюсь авторизоваться на https://login.live.com| Код |
static Encoding GetResponseEncoding(HttpWebResponse resp) { Encoding enc = null; { Match m = Regex.Match(resp.ContentType, @"charset=(?<enc>.+?)\s*(?:;|$)", RegexOptions.IgnoreCase); if (m.Success) enc = Encoding.GetEncoding(m.Groups["enc"].Value); } return enc; }
string data, action; CookieContainer cc = new CookieContainer(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://login.live.com/"); req.CookieContainer = cc; Encoding enc;
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) { enc = GetResponseEncoding(resp); if (enc != null) using (StreamReader sr = new StreamReader(resp.GetResponseStream(), enc)) data = sr.ReadToEnd(); else using (StreamReader sr = new StreamReader(resp.GetResponseStream())) data = sr.ReadToEnd(); }
Match m = Regex.Match(data, @"< *form.*?>.*?< */form.*?>", RegexOptions.Singleline | RegexOptions.IgnoreCase); if (!m.Success) throw new InvalidOperationException();
{ Match mm = Regex.Match(m.Value, @"< *form .*?action=(""|')(?<action>.*?)(""|').*?>", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase); if (mm.Success) action = HttpUtility.UrlDecode(mm.Groups["action"].Value); else throw new InvalidOperationException(); } m = Regex.Match(m.Value, @"<input *(?:type=(?:""|\')hidden(?:""|\')).*?/>", RegexOptions.IgnoreCase | RegexOptions.Singleline); NameValueCollection nvc = new NameValueCollection(); while (m.Success) { string pattern = @"<input(?<params>.*?)/>"; Match mm = Regex.Match(m.Value, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); if (mm.Success) { Match m2 = Regex.Match(mm.Groups["params"].Value, @"(?<name>\w+)\s*=\s*(""|')(?<value>.*?)(""|')", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); string name = null, value = null; while (m2.Success) { if (m2.Groups["name"].Value == "name") name = m2.Groups["value"].Value; else if (m2.Groups["name"].Value == "value") value = m2.Groups["value"].Value; else { m2 = m2.NextMatch(); if (!m2.Success) { name = value = null; break; } continue; } if (name != null && value != null) { nvc.Add(name, value); name = value = null; break; } m2 = m2.NextMatch(); if (!m2.Success) { name = value = null; break; } } } m = m.NextMatch(); }
nvc.Add("login", "[email protected]"); nvc.Add("passwd", "mypassword");
StringBuilder sb = new StringBuilder(); string[] keys = nvc.AllKeys; sb.Append(string.Format("{0}={1}", HttpUtility.UrlEncode(keys[0]), HttpUtility.UrlEncode(nvc[0])));
for (int i = 1; i < nvc.Count; ++i) sb.Append(string.Format("&{0}={1}", HttpUtility.UrlEncode(keys[i]), HttpUtility.UrlEncode(nvc[i]))); string reqp = sb.ToString(); byte[] sendp = Encoding.ASCII.GetBytes(reqp);
req = (HttpWebRequest)WebRequest.Create(action); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = sendp.Length; req.CookieContainer = cc;
using (Stream st = req.GetRequestStream()) st.Write(sendp, 0, sendp.Length); using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) { enc = GetResponseEncoding(resp); if (enc != null) using (StreamReader sr = new StreamReader(resp.GetResponseStream(), enc)) data = sr.ReadToEnd(); else using (StreamReader sr = new StreamReader(resp.GetResponseStream())) data = sr.ReadToEnd(); } using (StreamWriter sw = new StreamWriter("answer.html")) sw.Write(data);
|
вроде бы все поля отправляю, но тем не менее он говорит что неправильный адрес или пароль, хотя эти данные верные в чем может быть проблема
|