How to send an image as part of a multi-page POST request - Java HtmlUnit

I am trying to use Java to send captcha to decaptcher.com. Decaptcher doesn't really explain very well how to use their APIs, so I'm trying to figure out how to use an HTTP POST request to send a captcha. Here is an example of the code I received from my site:

<form method="post" action="http://poster.decaptcher.com/" enctype="multipart/form-data"> <input type="hidden" name="function" value="picture2"> <input type="text" name="username" value="client"> <input type="text" name="password" value="qwerty"> <input type="file" name="pict"> <input type="text" name="pict_to" value="0"> <input type="text" name="pict_type" value="0"> <input type="submit" value="Send"> </form> 

I have to send such a request to the web server and get the string returned to me. Here is my attempt to implement this in Java.

 public String getDecaptcherAnswer(String username, String password){ try{ URL decaptcherPostURL = new URL("http://poster.decaptcher.com/"); WebRequestSettings request = new WebRequestSettings(decaptcherPostURL, HttpMethod.POST); request.setEncodingType(FormEncodingType.MULTIPART); ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new NameValuePair("function", "picture2")); params.add(new NameValuePair("username", username)); params.add(new NameValuePair("password", password)); //I added this block in File file = new File("captcha.png"); params.add(new KeyDataPair("pict", capFile, "png", "utf-8")); //---------------------- params.add(new NameValuePair("pict_to", "0")); params.add(new NameValuePair("pict_type", "0")); request.setRequestParameters(params); request.setUrl(decaptcherPostURL); HtmlPage page = webClient.getPage(request); System.out.println(page.asText()); System.out.println("--------------------------------------"); System.out.println(page.asXml()); return page.asText(); }catch (Exception e){ e.printStackTrace(); return null; } } 

Should I set the pict value for the File object instead of a line indicating where captcha is stored? (captcha.png is the name of the image I'm trying to present).

+4
source share
3 answers

There is a higher level mechanism for sending this file, you do not need to create WebRequestSettings and set its individual values.

You should place this static html somewhere and do something like below.

If you still have a problem, send a bug report to HtmlUnit.

BTW, HtmlUnit 2.8 is about to come out, try it.

 WebClient webClient = new WebClient(); HtmlPage page = webClient.getPage("http://some_host/test.html"); HtmlForm form = page.getForms().get(0); form.getInputByName("username").setValueAttribute(username); form.getInputByName("password").setValueAttribute(password); form.getInputByName("pict_to").setValueAttribute("0"); form.getInputByName("pict_type").setValueAttribute("0"); form.getInputByName("pict").setValueAttribute("full_path_to_captcha_png"); form.<HtmlFileInput>getInputByName("pict").setContentType("image/png");//optional HtmlPage page2 = form.getInputByValue("Send").click(); 
+3
source

You should not use NameValuePair for this, but its subclass KeyDataPair . This way you can specify the file to download.

The following should work:

 new KeyDataPair("pict", new File(fileName), "image/png", "utf-8"); 

The content type parameter is the MIME file type . Since you are loading a PNG file, it should be image/png .

+1
source

Here is what I tried to print:

 File file = new File("captcha.png"); params.add(new KeyDataPair("pict", capFile, "png", "utf-8")); 

PNG files encoded using UTF-8? So I would indicate KeyDataPair to enter the file? I think that I am either setting the wrong contentType, or the wrong charSet, or both. Should I put them in all caps?

0
source

All Articles