Пример #1
0
        public IActionResult GetCode()
        {
            //var (code, bytes) = CaptchaHelper.GenVCode(4);

            //// code handle logic
            //System.Console.WriteLine(code);
            //Console.WriteLine(++count);

            //return File(bytes, "image/png");

            //_captchaService.SetOptions(new CaptchaOptions()
            //{
            //    Height = 35,
            //    Width = 90
            //});

            var text  = _captchaService.GetRandomText(5);
            var bytes = _captchaService.GetCaptcha(text);

            Console.WriteLine(text);
            Console.WriteLine(++count);
            return(File(bytes, "image/png"));
        }
Пример #2
0
        public bool Post(Advertisement ad)
        {
            Dictionary <string, string> data = new Dictionary <string, string>();

            data.Add("seller_name", ad.User.Name);
            data.Add("manager", (ad.User.Manager == null) ? "" : ad.User.Manager);
            data.Add("phone", ad.User.Phone);
            data.Add("email", ad.User.Email);
            data.Add("allow_mails", (ad.User.DenyEmails) ? "1" : "");

            data.Add("location_id", ad.Location.City.Id.ToString());
            data.Add("metro_id", (ad.Location.Metro == null) ? "" : ad.Location.Metro.Id.ToString());
            data.Add("district_id", (ad.Location.District == null) ? "" : ad.Location.District.Id.ToString());
            data.Add("road_id", (ad.Location.Road == null) ? "" : ad.Location.Road.Id.ToString());

            data.Add("category_id", ad.CategoryId.ToString());
            foreach (KeyValuePair <int, int> x in ad.Parameters)
            {
                data.Add("params[" + x.Key.ToString() + "]", x.Value.ToString());
            }

            data.Add("title", ad.Title);
            data.Add("description", ad.Body);

            if (ad.Coordinates != null)
            {
                data.Add("coords[lat]", ad.Coordinates.Latitude);
                data.Add("coords[lng]", ad.Coordinates.Longitude);
                data.Add("coords[zoom]", ad.Coordinates.Zoom);
            }

            data.Add("price", ad.Price.ToString());

            data.Add("service_code", "free");
            data.Add("main_form_submit", "Продолжить с пакетом «Обычная продажа»");

            foreach (Image image in ad.Images)
            {
                data.Add("images[]", PostImage(image));
            }

            AvitoWebClient web = new AvitoWebClient(auth.CredsCont);

            web.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            web.Headers.Add("Referer", url1);

            try
            {
                web.UploadString(url1, Stringify(data));
            }
            catch (Exception ex)
            {
                throw new AvitoNetworkException();
            }
            if (!web.ResponseUrl.Contains("confirm"))
            {
                throw new WrongAdvertisementException();
            }

            string date = ((int)((DateTime.Now - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds)).ToString();

            HttpWebRequest req = HttpWebRequest.Create(captchaUrl + date) as HttpWebRequest;

            req.Referer         = url2;
            req.CookieContainer = auth.CredsCont;
            HttpWebResponse res;

            try
            {
                res = req.GetResponse() as HttpWebResponse;
            }
            catch (Exception ex)
            {
                return(false);
            }
            Image im = new Bitmap(res.GetResponseStream());

            string cap;

            try
            {
                cap = captcha.GetCaptcha(im);
            }
            catch (Exception ex)
            {
                return(false);
            }

            data.Clear();
            data.Add("captcha", cap);
            data.Add("service", "0");
            data.Add("subscribe-position", "0");

            web.Headers.Add("Referer", url2);
            web.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            try
            {
                web.UploadString(url2, Stringify(data));
            }
            catch (Exception ex)
            {
                throw new AvitoNetworkException();
            }
            if (!web.ResponseUrl.Contains("finish"))
            {
                throw new CaptchaException();
            }
            return(true);
        }