public bool http_auth_vk(string login, string pass)
{
//*****************************
//Получаем action_url
//*****************************
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
System.Net.WebRequest reqGET = System.Net.WebRequest.Create("http://m.vk.com/");
System.Net.WebResponse resp = reqGET.GetResponse();
System.IO.Stream stream = resp.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(stream);
string s = sr.ReadToEnd();
//*****************************
//Парсим
//*****************************
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(s);
HtmlNode bodyNode = doc.DocumentNode.SelectSingleNode("//div[@class='cont']/form");
string result1 = bodyNode.Attributes["action"].Value;
//*****************************
//POST запрос
//*****************************
var cookies = new CookieContainer();
ServicePointManager.Expect100Continue = false;
var request = (HttpWebRequest)WebRequest.Create(result1);
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var requestStream = request.GetRequestStream())
using (var writer = new StreamWriter(requestStream))
{
writer.Write("email=" + login + "&pass=" + pass);
}
using (var responseStream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(responseStream))
{
var result = reader.ReadToEnd();
//*****************************
//Парсим, поиск ID
//*****************************
HtmlAgilityPack.HtmlDocument doc2 = new HtmlAgilityPack.HtmlDocument();
doc2.LoadHtml(result);
string result2;
try
{
HtmlNode bodyNode2 = doc2.DocumentNode.SelectSingleNode("//div[@class='user_wrap']/a");
result2 = bodyNode2.Attributes["href"].Value.Substring(3);
//Если ID найден, то авторизация удалась
return true;
}
catch
{
//Если ID не найден, то авторизация не удалась
MessageBox.Show("Ошибка авторизации, проверьте правильность введённых данных.\nВозможно там капча.");
return false;
}
}
}
string AuthUrl = "https://m.vk.com/login";
string logoutId = "act=logout";
string MainUrl = "https://vk.com";
******************************
httpHandler = new HttpClientHandler
{
CookieContainer = new CookieContainer(),
AllowAutoRedirect = true,
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
};
httpClient = new HttpClient(httpHandler) {Timeout = TimeSpan.FromSeconds(150)};
httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
httpClient.DefaultRequestHeaders.Add("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
*************************************
Uri authUri = new Uri(AuthUrl);
string data = await httpClient.GetStringAsync(authUri);
IsAuth = data.Contains(logoutId);
if (!IsAuth)
{
string result = data.Substring("<form method=\"post\" action=\"", "\"");//достаем ссылку для авторизации
authUri = new Uri(result);
var body = new Dictionary<string, string> {{"act", "login"}, {"email", Email}, {"pass", Pass}};//тут заполняем логин и пароль
var response = await httpClient.PostAsync(authUri, new FormUrlEncodedContent(body));
var page = await response.Content.ReadAsStringAsync();
IsAuth = page.Contains(logoutId);//если вошли на странице появится ссыль для выхода :)
Uri mainUri = new Uri(MainUrl);//переходим на главную страницу
data = await httpClient.GetStringAsync(mainUri);
}
return IsAuth;