Beispiel #1
0
        private Post Submit(SubmitData data)
        {
            if (Reddit.User == null)
            {
                throw new RedditException("No user logged in.");
            }
            var request = WebAgent.CreatePost(SubmitLinkUrl);

            WebAgent.WritePostBody(request.GetRequestStream(), data);

            var            json   = WebAgent.ExecuteRequest(request);
            ICaptchaSolver solver = Reddit.CaptchaSolver;

            if (json["errors"].Any() && json["errors"][0][0].ToString() == "BAD_CAPTCHA" &&
                solver != null)
            {
                data.Iden = json["captcha"].ToString();
                CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(data.Iden));

                // We throw exception due to this method being expected to return a valid Post object, but we cannot
                // if we got a Captcha error.
                if (captchaResponse.Cancel)
                {
                    throw new CaptchaFailedException("Captcha verification failed when submitting " + data.Kind + " post");
                }

                data.Captcha = captchaResponse.Answer;
                return(Submit(data));
            }

            return(new Post(Reddit, json, WebAgent));
        }
        private Post Submit(SubmitData data)
        {
            if (Reddit.User == null)
            {
                throw new RedditException("No user logged in.");
            }
            var request = WebAgent.CreatePost(SubmitLinkUrl);

            WebAgent.WritePostBody(request.GetRequestStream(), data);

            var response = request.GetResponse();
            var result   = WebAgent.GetResponseString(response.GetResponseStream());
            var json     = JToken.Parse(result);

            ICaptchaSolver solver = Reddit.CaptchaSolver;

            if (json["json"]["errors"].Any() && json["json"]["errors"][0][0].ToString() == "BAD_CAPTCHA" &&
                solver != null)
            {
                data.Iden = json["json"]["captcha"].ToString();
                CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(data.Iden));

                // We throw exception due to this method being expected to return a valid Post object, but we cannot
                // if we got a Captcha error.
                if (captchaResponse.Cancel)
                {
                    throw new CaptchaFailedException("Captcha verification failed when submitting " + data.Kind + " post");
                }

                data.Captcha = captchaResponse.Answer;
                return(Submit(data));
            }
            else if (json["json"]["errors"].Any() && json["json"]["errors"][0][0].ToString() == "ALREADY_SUB")
            {
                throw new DuplicateLinkException(String.Format("Post failed when submitting.  The following link has already been submitted: {0}", SubmitLinkUrl));
            }

            return(new Post(Reddit, json["json"], WebAgent));
        }
Beispiel #3
0
        private Post Submit(SubmitData data)
        {
            if (Reddit.User == null)
                throw new RedditException("No user logged in.");
            var request = WebAgent.CreatePost(SubmitLinkUrl);

            WebAgent.WritePostBody(request.GetRequestStream(), data);

            var response = request.GetResponse();
            var result = WebAgent.GetResponseString(response.GetResponseStream());
            var json = JToken.Parse(result);

            ICaptchaSolver solver = Reddit.CaptchaSolver;
            if (json["json"]["errors"].Any() && json["json"]["errors"][0][0].ToString() == "BAD_CAPTCHA"
                && solver != null)
            {
                data.Iden = json["json"]["captcha"].ToString();
                CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(data.Iden));

                // We throw exception due to this method being expected to return a valid Post object, but we cannot
                // if we got a Captcha error.
                if (captchaResponse.Cancel)
                    throw new CaptchaFailedException("Captcha verification failed when submitting " + data.Kind + " post");

                data.Captcha = captchaResponse.Answer;
                return Submit(data);
            }
            else if (json["json"]["errors"].Any() && json["json"]["errors"][0][0].ToString() == "ALREADY_SUB")
            {
                throw new DuplicateLinkException(String.Format("Post failed when submitting.  The following link has already been submitted: {0}", SubmitLinkUrl));
            }

            return new Post(Reddit, json["json"], WebAgent);
        }
Beispiel #4
0
        private Post Submit(SubmitData data)
        {
            if (Reddit.User == null)
                throw new RedditException("No user logged in.");
            var request = WebAgent.CreatePost(SubmitLinkUrl);

            WebAgent.WritePostBody(request.GetRequestStream(), data);

            var json = WebAgent.ExecuteRequest(request);
            ICaptchaSolver solver = Reddit.CaptchaSolver;
            if (json["errors"].Any() && json["errors"][0][0].ToString() == "BAD_CAPTCHA"
                && solver != null)
            {
                data.Iden = json["captcha"].ToString();
                CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(data.Iden));

                // We throw exception due to this method being expected to return a valid Post object, but we cannot
                // if we got a Captcha error.
                if (captchaResponse.Cancel)
                    throw new CaptchaFailedException("Captcha verification failed when submitting " + data.Kind + " post");

                data.Captcha = captchaResponse.Answer;
                return Submit(data);
            }

            return new Post(Reddit, json, WebAgent);
        }