Example #1
0
        /// <summary>
        /// Post запрос из формы.
        /// </summary>
        /// <param name="form">Форма.</param>
        /// <param name="host">Хост.</param>
        /// <param name="port">Порт.</param>
        /// <param name="proxyLogin">Логин прокси-сервера</param>
        /// <param name="proxyPassword">Пароль прокси-сервера</param>
        /// <returns>Результат</returns>
        public static WebCallResult Post(WebForm form, string host = null, int?port = null, string proxyLogin = null, string proxyPassword = null)
        {
            var call = new WebCall(form.ActionUrl, form.Cookies, host, port, proxyLogin, proxyPassword);

            var request = call.Request;

            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            var formRequest = form.GetRequest();

            request.ContentLength = formRequest.Length;
            request.Referer       = form.OriginalUrl;
            request.GetRequestStream().Write(formRequest, 0, formRequest.Length);
            request.AllowAutoRedirect = false;

            return(call.MakeRequest(host, port, proxyLogin, proxyPassword));
        }
Example #2
0
        /// <summary>
        /// Авторизация на сервере ВК
        /// </summary>
        /// <param name="appId">Идентификатор приложения</param>
        /// <param name="email">Логин - телефон или эл. почта</param>
        /// <param name="password">Пароль</param>
        /// <param name="settings">Уровень доступа приложения</param>
        /// <param name="code">Код двухфакторной авторизации</param>
        /// <param name="captchaSid">Идентификатор капчи</param>
        /// <param name="captchaKey">Текст капчи</param>
        /// <param name="host">Имя узла прокси-сервера.</param>
        /// <param name="port">Номер порта используемого Host.</param>
        /// <param name="proxyLogin">Логин для прокси-сервера.</param>
        /// <param name="proxyPassword">Пароль для прокси-сервера</param>
        /// <returns>Информация об авторизации приложения</returns>
        public VkAuthorization Authorize(ulong appId, string email, string password, Settings settings, Func <string> code = null, long?captchaSid = null, string captchaKey = null,
                                         string host = null, int?port = null, string proxyLogin = null, string proxyPassword = null)
        {
            _host          = string.IsNullOrWhiteSpace(host) ? null : host;
            _port          = port;
            _proxyLogin    = string.IsNullOrWhiteSpace(proxyLogin) ? null : proxyLogin;
            _proxyPassword = string.IsNullOrWhiteSpace(proxyPassword) ? null : proxyPassword;

            var authorizeUrl       = CreateAuthorizeUrlFor(appId, settings, Display.Wap);
            var authorizeUrlResult = WebCall.MakeCall(authorizeUrl, host, port, proxyLogin, proxyPassword);

            // Заполнить логин и пароль
            var loginForm = WebForm.From(authorizeUrlResult).WithField("email").FilledWith(email).And().WithField("pass").FilledWith(password);

            if (captchaSid.HasValue)
            {
                loginForm.WithField("captcha_sid").FilledWith(captchaSid.Value.ToString()).WithField("captcha_key").FilledWith(captchaKey);
            }
            var loginFormPostResult = WebCall.Post(loginForm, host, port, proxyLogin, proxyPassword);

            // Заполнить код двухфакторной авторизации
            if (code != null)
            {
                var codeForm = WebForm.From(loginFormPostResult).WithField("code").FilledWith(code());
                loginFormPostResult = WebCall.Post(codeForm, host, port);
            }

            var authorization = VkAuthorization.From(loginFormPostResult.ResponseUrl);

            if (authorization.CaptchaId.HasValue)
            {
                throw new CaptchaNeededException(authorization.CaptchaId.Value, "http://api.vk.com/captcha.php?sid=" + authorization.CaptchaId.Value);
            }
            if (!authorization.IsAuthorizationRequired)
            {
                return(authorization);
            }

            // Отправить данные
            var authorizationForm           = WebForm.From(loginFormPostResult);
            var authorizationFormPostResult = WebCall.Post(authorizationForm, host, port, proxyLogin, proxyPassword);

            return(VkAuthorization.From(authorizationFormPostResult.ResponseUrl));
        }