Авторизация на сайте через программу - C#
Формулировка задачи:
Всем доброго времени суток.
Уважаемые участники cyberforum.ru , я с недавних пор занимаюсь программированием на C# и у меня возникла проблема и я не знаю как ее решить.
Мне нужно сделать что бы в моём лаунчере была авторизация, а то есть:
Логин:
Пароль:
и кнопка вход.
Но вот самое интересное, мне надо что бы данные логин и пароль которые ввёл человек ввелись на сайте к примеру lala.ru и если всё хорошо, то у человека там открывается ещё 1 форма [Форму я сделаю], а если не авторизовалось там на сайте, то в программе человеку выдаёт ошибку.)
Если можете помочь, то вот Skype:
Я человек адекватный.
Решение задачи: «Авторизация на сайте через программу»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public static CookieContainer container;
- public Form1()
- {
- InitializeComponent();
- }
- public static string getToken(string html)
- {
- string pattern = "name=\"csrf_token_name\" value=\"(.*)\"";
- Match res;
- res = Regex.Match(html, pattern);
- return res.Groups[1].ToString();
- }
- public static string Post(string url, string data, bool clearCookie, ref CookieContainer container, bool allow_redirect = true, WebProxy proxy = null)
- {
- string output = null;
- try
- {
- HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
- if (clearCookie) WebReq.CookieContainer = new CookieContainer();
- else WebReq.CookieContainer = container;
- //byte[] buffer = Encoding.ASCII.GetBytes(data);
- byte[] buffer = Encoding.UTF8.GetBytes(data);
- WebReq.Method = "POST";
- WebReq.AllowAutoRedirect = allow_redirect;
- WebReq.ContentType = "application/x-www-form-urlencoded";
- WebReq.ContentLength = buffer.Length;
- Stream PostData = WebReq.GetRequestStream();
- PostData.Write(buffer, 0, buffer.Length);
- PostData.Close();
- HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
- Stream Answer = WebResp.GetResponseStream();
- container = WebReq.CookieContainer;
- StreamReader _Answer = new StreamReader(Answer, Encoding.UTF8);
- output = _Answer.ReadToEnd();
- return output.Trim() + "\n";
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- public static string Get(string url, bool clearCookie, ref CookieContainer container, WebProxy proxy = null)
- {
- string output = null;
- try
- {
- HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
- if (clearCookie) WebReq.CookieContainer = new CookieContainer();
- else WebReq.CookieContainer = container;
- WebReq.Method = "GET";
- WebReq.AllowAutoRedirect = true;
- WebReq.ContentType = "application/x-www-form-urlencoded";
- HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
- Stream Answer = WebResp.GetResponseStream();
- container = WebReq.CookieContainer;
- StreamReader _Answer = new StreamReader(Answer, Encoding.UTF8);
- output = _Answer.ReadToEnd();
- return output.Trim() + "\n";
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string page = Get("http://scarius.ru/wow/login", false, ref container);
- string token = getToken(page);
- string data = "csrf_token_name="+token+"&login_username={тут логин}&login_password={тут пароль}&login_remember=on&login_submit=%D0%92%D0%BE%D0%B9%D1%82%D0%B8%21";
- //MessageBox.Show(token);
- string result = Post("http://scarius.ru/wow/login", data, false, ref container);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д