2010-08-01 85 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

相關問題