Exemplo n.º 1
0
        /**
         * <see cref="M:DeathByCaptcha.Client.Upload(byte[])"/>
         */
        public override Captcha Upload(byte[] img)
        {
            string boundary = BitConverter.ToString(
                (new SHA1CryptoServiceProvider()).ComputeHash(
                    Encoding.ASCII.GetBytes(DateTime.Now.ToString("G"))
                )
            ).Replace("-", String.Empty);

            Hashtable args = this.Credentials;
            args["swid"] = Convert.ToString(Client.SoftwareVendorId);
            string[] rawArgs = new string[args.Count + 2];
            int i = 0;
            foreach (DictionaryEntry e in args) {
                string v = (string)e.Value;
                rawArgs[i++] = String.Join("\r\n", new string[] {
                    "--" + boundary,
                    "Content-Disposition: form-data; name=\"" + (string)e.Key + "\"",
                    "Content-Length: " + v.Length,
                    "",
                    v
                });
            }
            rawArgs[i++] = String.Join("\r\n", new string[] {
                "--" + boundary,
                "Content-Disposition: form-data; name=\"captchafile\"; filename=\"captcha.jpeg\"",
                "Content-Type: application/octet-stream",
                "Content-Length: " + img.Length,
                ""
            });

            byte[] hdr = Encoding.ASCII.GetBytes(String.Join("\r\n", rawArgs));
            byte[] ftr = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            byte[] body = new byte[hdr.Length + img.Length + ftr.Length];
            hdr.CopyTo(body, 0);
            img.CopyTo(body, hdr.Length);
            ftr.CopyTo(body, hdr.Length + img.Length);

            Captcha c = new Captcha(this.Call(
                "captcha",
                body,
                "multipart/form-data; boundary=" + boundary
            ));
            return c.Uploaded ? c : null;
        }
Exemplo n.º 2
0
 /**
  * <see cref="M:DeathByCaptcha.Client.Upload(byte[])"/>
  */
 public override Captcha Upload(byte[] img)
 {
     Hashtable args = new Hashtable();
     args["swid"] = Client.SoftwareVendorId;
     args["captcha"] = Convert.ToBase64String(img);
     Captcha c = new Captcha(this.Call("upload", args));
     return c.Uploaded ? c : null;
 }
Exemplo n.º 3
0
 /**
  * <summary>Wait for a CAPTCHA to be solved.</summary>
  * <param name="captcha">Uploaded CAPTCHA.</param>
  * <param name="timeout">Solving timeout (in seconds).</param>
  * <returns>CAPTCHA if solved, null otherwise.</returns>
  */
 protected Captcha Poll(Captcha captcha, int timeout)
 {
     if (null != captcha) {
         DateTime deadline =
             DateTime.Now.AddSeconds(0 < timeout
                 ? timeout
                 : Client.DefaultTimeout);
         while (deadline > DateTime.Now && !captcha.Solved) {
             Thread.Sleep(Client.PollsInterval * 1000);
             try {
                 captcha = this.GetCaptcha(captcha);
             } catch (System.Exception e) {
                 if (this.Verbose) {
                     Console.WriteLine(DateTime.Now.Ticks + " POLL " + e.Message);
                 }
                 return null;
             }
         }
         if (captcha.Solved && captcha.Correct) {
             return captcha;
         }
     }
     return null;
 }
Exemplo n.º 4
0
 /**
  * <summary>Report an incorrectly solved CAPTCHA.</summary>
  * <param name="captcha">CAPTCHA.</param>
  * <returns>true on success.</returns>
  */
 public bool Report(Captcha captcha)
 {
     return this.Report(captcha.Id);
 }
Exemplo n.º 5
0
 /**
  * <param name="captcha">CAPTCHA.</param>
  * <returns>Uploaded CAPTCHA text, null if not found or not solved yet.</returns>
  */
 public string GetText(Captcha captcha)
 {
     return this.GetCaptcha(captcha).Text;
 }
Exemplo n.º 6
0
 /**
  * <param name="captcha">CAPTCHA.</param>
  * <returns>Uploaded CAPTCHA if exists, null otherwise.</returns>
  */
 public Captcha GetCaptcha(Captcha captcha)
 {
     return this.GetCaptcha(captcha.Id);
 }