It is necessary to use the ruCaptha service for captcha recognition. Description API on their website.

In short, they need to POST request to send a picture encoded in base64 and then in URLEncode.

When it is done like this

String url = "http://rucaptcha.com/in.php"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //add reuqest header con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String urlParameters = "method=base64&key=*****************&"+"body="+img+"&submit=загрузить и получить ID"; // Send post request con.setDoOutput(true); try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) { wr.writeBytes(urlParameters); wr.flush(); } StringBuffer response; try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) { String inputLine; response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } } resp = response.toString(); resp_code = resp.substring(3); System.out.println("ID заказа : "+resp_code); 

That all works. But you need to do this with Apache HTTP Client 4.5.2. Wrote such code

 HttpPost httpPost = new HttpPost("http://rucaptcha.com/in.php"); java.util.List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("method", "base64")); nvps.add(new BasicNameValuePair("key", "***********")); nvps.add(new BasicNameValuePair("body", img)); nvps.add(new BasicNameValuePair("submit", "загрузить и получить ID")); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); httpPost.setConfig(config); CloseableHttpResponse response = httpClient.execute(httpPost); String entityString = EntityUtils.toString(response.getEntity()); System.out.println(entityString); resp_code = entityString.substring(3); //System.out.println(resp); System.out.println("ID заказа : "+resp_code); EntityUtils.consume(response.getEntity()); 

But I get the error ERROR_IMAGE_TYPE_NOT_SUPPORTED. Googling understood that you need to send POST type multipart / form-data. But how to do it specifically in this situation?

  • In the first code example, there is no multipart / form-data. I suppose the problem is that in the second code fragment, the img variable contains the value encoded in base64 + url-encode, but it should just be base64 (apache httpclient itself makes url-encode). - Roman

2 answers 2

You do not need URL-coding img before transferring to HttpClient - this operation will be done automatically for you behind the scenes. Remove URL coding for img and it will work.

    If you have not found the answer yet, you can add the file in the multipart / form-data in the apache client like this:

      FileBody fileBody = new FileBody(new File("FUBAR"), ContentType.MULTIPART_FORM_DATA); HttpEntity httpEntity = MultipartEntityBuilder.create() .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addPart("file", fileBody).build(); postRequest.setEntity(httpEntity); response = httpClient.execute(postRequest);