Beispiel #1
0
        /// <summary>
        /// Получение json по url-адресу
        /// </summary>
        /// <param name="url">Адрес получения json</param>
        /// <returns>Строка в формате json</returns>
        public string GetJson(string url)
        {
            var separatorPosition = url.IndexOf('?');
            var methodUrl         = separatorPosition < 0 ? url : url.Substring(0, separatorPosition);
            var parameters        = separatorPosition < 0 ? string.Empty : url.Substring(separatorPosition + 1);

            return(WebCall.PostCall(methodUrl, parameters, _host, _port, _proxyLogin, _proxyPassword).Response);
        }
Beispiel #2
0
        /// <summary>
        /// Асинхронное получение json по url-адресу
        /// </summary>
        /// <param name="url">Адрес получения json</param>
        /// <returns>Строка в формате json</returns>
        public async Task <string> GetJsonAsync(string url)
        {
            // todo refactor this shit
            var    separatorPosition = url.IndexOf('?');
            string methodUrl         = separatorPosition < 0 ? url : url.Substring(0, separatorPosition);
            string parameters        = separatorPosition < 0 ? string.Empty : url.Substring(separatorPosition + 1);

            return(await WebCall.PostCallAsync(url, parameters));
        }
Beispiel #3
0
        /// <summary>
        /// Пере адресация.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="host">Хост.</param>
        /// <param name="port">Порт.</param>
        /// <param name="proxyLogin">Логин прокси-сервера</param>
        /// <param name="proxyPassword">Пароль прокси-сервера</param>
        /// <returns>Результат</returns>
        private WebCallResult RedirectTo(string url, string host = null, int?port = null, string proxyLogin = null, string proxyPassword = null)
        {
            var call = new WebCall(url, Result.Cookies, host, port, proxyLogin, proxyPassword);

            var request = call.Request;

            request.Method      = "GET";
            request.ContentType = "text/html";
            request.Referer     = Request.Referer;

            return(call.MakeRequest(host, port, proxyLogin, proxyPassword));
        }
Beispiel #4
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));
        }
Beispiel #5
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));
        }
Beispiel #6
0
        /// <summary>
        /// Выполнить POST запрос.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="parameters">Параметры запроса.</param>
        /// <param name="host">Хост.</param>
        /// <param name="port">Порт.</param>
        /// <param name="proxyLogin">Логин прокси-сервера</param>
        /// <param name="proxyPassword">Пароль прокси-сервера</param>
        /// <returns>Результат</returns>
        public static WebCallResult PostCall(string url, string parameters, string host = null, int?port = null, string proxyLogin = null, string proxyPassword = null)
        {
            var call = new WebCall(url, new Cookies(), host, port, proxyLogin, proxyPassword)
            {
                Request =
                {
                    Method      = "POST",
                    ContentType = "application/x-www-form-urlencoded"
                }
            };

            var data = Encoding.UTF8.GetBytes(parameters);

            call.Request.ContentLength = data.Length;

            using (var requestStream = call.Request.GetRequestStream())
                requestStream.Write(data, 0, data.Length);

            return(call.MakeRequest(host, port, proxyLogin, proxyPassword));
        }
Beispiel #7
0
        /// <summary>
        /// Выполнить запрос.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="host">Хост.</param>
        /// <param name="port">Порт.</param>
        /// <param name="proxyLogin">Логин прокси-сервера</param>
        /// <param name="proxyPassword">Пароль прокси-сервера</param>
        /// <returns>Результат</returns>
        public static WebCallResult MakeCall(string url, string host = null, int?port = null, string proxyLogin = null, string proxyPassword = null)
        {
            var call = new WebCall(url, new Cookies(), host, port, proxyLogin, proxyPassword);

            return(call.MakeRequest(host, port, proxyLogin, proxyPassword));
        }