Sending a captcha to the antigate service (java). ERROR ZERO CAPTCHA FILESIZE ERROR

Hello, I want to send a captcha to the antigate service, I use the post and base64 method as stated in the documentation - https://anti-captcha.com/apidoc
I get the error ERROR_ZERO_CAPTCHA_FILESIZE
In the post request parameter, I give a key(my access token to the account-here note 123456789) and a link to the captcha
I encode in base 64 and send
I do not understand why the service does not accept the image, it seems to encode correctly, on the Internet in the online decoder I inserted the image code in base64 and received my captcha in response
Here is my code :

    private static void sendPost(String encodedImage,String key) throws Exception {
        String url = "http://anti-captcha.com/in.php?";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        String urlParameters = "key="+key+"&file="+encodedImage;    
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        System.out.println(response.toString());

    }

    public static String encodeToString(BufferedImage image, String type) {  
        String imageString = null;  
        ByteArrayOutputStream bos = new ByteArrayOutputStream();  

        try {  
            ImageIO.write(image, type, bos);  
            byte[] imageBytes = bos.toByteArray();  

            BASE64Encoder encoder = new BASE64Encoder();  
            imageString = encoder.encode(imageBytes);  

            bos.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return imageString;  
    }  

public static void main(String[] args) throws Exception{

    URL url = new URL("http://api.vk.com/captcha.php?sid=579356847070&s=1");
    BufferedImage img = ImageIO.read(url);
    String encodedCaptcha=encodeToString(img, "jpg");
    String key="123456789";
    sendPost(key,encodedCaptcha);


    }
}
Author: alex-rudenkiy, 2016-02-24

2 answers

As stated in the documentation

First, you don't pass method.

Second, (although this may just be an error in the documentation example), both methods should be in multipart/form-data. You are at best sending application/x-www-form-urlencoded if setRequestMethod("POST") exposes it. At the same time, apparently, without urlencode. Whereas base64 can normally contain the character = at the end - i.e. it will be interpreted incorrectly. This is what the documentation says:

IMPORTANT: Do not forget to encode the body with URLEncode, otherwise picture data will arrive scrambled.

Not familiar with Java, look for how the HTTP request body is formed normally.
Manual concatenation "key="+key+"&file="+encodedImage is not normal. Surely there is a standard method that accepts a list of parameters with values and generates the correct query body string.
Or, at least, how to do urlencode manually.

 3
Author: Мелкий, 2016-02-26 10:33:06

Perhaps you have mixed up the order of the arguments? Send it:

 sendPost(key,encodedCaptcha);

Accepts sendPost:

void sendPost(String encodedImage,String key)

And in place of EncodedImage you have a key, and where key is-EncodedImage

 0
Author: mass_, 2016-02-25 03:57:46