2010-08-01 219 views
4

我正尝试使用Java将验证码提交给decaptcher.com。 Decaptcher并没有很好的解释如何使用他们的API,所以我想弄清楚如何使用HTTP POST请求来提交验证码。下面是示例代码,我从他们的网站有:如何发送图片作为多部分POST请求的一部分 - Java HtmlUnit

<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> 

我应该发送POST请求一样,到Web服务器,并得到一个字符串返回给我。这是我尝试在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; 
     } 
} 

我应该将pict的值设置为File对象而不是指向captcha存储位置的String? (captcha.png是我想提交的图像的名称)。

回答

1

你不应该使用NameValuePair这个,但它的子类,KeyDataPair。这样您可以指定要上传的文件。

下面应该工作:

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

内容类型参数是MIME类型的文件。由于您正在上传PNG文件,因此应该是image/png

+0

请问我宣布KeyValuePair为: – Dylan 2010-08-03 01:57:18

+0

//假装我创建 “captcha.png” File对象调用的文件 新KeyValuePair( “PICT” 文件, “PNG”, “UTF-8”) 用UTF-8编码PNG文件吗? – Dylan 2010-08-03 02:00:59

+0

我添加了一个我认为应该可以工作的例子。我不确定utf-8字符集,也许你应该尝试一下。 – 2010-08-03 10:11:31

0

这里就是我试图键入:

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

编码使用UTF-8 PNG文件?那是我如何为文件输入指定KeyDataPair?我想我要么指定错误的contentType,要么指定错误的charSet,或者两者兼有。我应该把它们放在所有的帽子里吗?

+2

这不应该是一个单独的答案。这应该作为编辑添加到您的原始问题。 – 2010-08-03 10:13:30

3

有更高级别的机制发送该文件,您不需要创建WebRequestSettings并设置其各自的值。

你应该将该静态HTML托管在某个地方,并执行下面的操作。

如果您还有问题,请在HtmlUnit错误跟踪器中提交错误报告。

顺便说一句,HtmlUnit 2.8即将发布,试试吧。

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(); 
+0

您确定这将适用于POST请求吗? – Dylan 2010-08-05 02:04:13

+1

是的,HtmlUnit是为了让你头痛:),请用2.8测试 – 2010-08-07 07:39:16

相关问题