![](/img/trans.png)
[英]How to send multipart POST request using HttpURLConnection in java?
[英]How to send a picture as part of a multipart POST request - Java HtmlUnit
我正在嘗試使用Java向captaptcher.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對象而不是指向驗證碼存儲位置的String嗎? (captcha.png是我試圖提交的圖片的名稱)。
有一個更高級別的機制來發送該文件,您不需要創建WebRequestSettings
並設置其各自的值。
你應該在某個地方托管那個靜態html並執行類似下面的操作。
如果您仍有問題,請在HtmlUnit錯誤跟蹤器中提交錯誤報告。
BTW,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();
你不應該為它使用NameValuePair
,而是它的子類KeyDataPair
。 這樣您就可以指定要上載的文件。
以下應該有效:
new KeyDataPair("pict", new File(fileName), "image/png", "utf-8");
內容類型參數是文件的MIME類型。 由於您要上傳PNG文件,因此它應該是image/png
。
這是我試圖輸入的內容:
File file = new File("captcha.png");
params.add(new KeyDataPair("pict", capFile, "png", "utf-8"));
PNG文件是用UTF-8編碼的嗎? 這是我如何指定文件輸入的KeyDataPair? 我想我要么指定錯誤的contentType或錯誤的charSet,要么兩者都指定。 我應該把它們全部蓋上嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.