Java を使用してキャプチャを decaptcher.com に送信しようとしています。Decaptcher は API の使用方法をうまく説明していないので、HTTP POST リクエストを使用してキャプチャを送信する方法を理解しようとしています。これは私が彼らのウェブサイトから入手したサンプルコードです:
<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>
そのような投稿要求を 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.png は、送信しようとしている画像の名前です)。