如何发送图片作为多部分 POST 请求的一部分 - Java HtmlUnit
我正在尝试使用 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>
我应该向网络服务器发送这样的发布请求,并获取返回给我的字符串。这是我在 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 是我尝试提交的图像的名称)。
I am trying to use Java to submit a captcha to decaptcher.com. Decaptcher doesn't really do a good job of explaining how to use their API's, so I am trying to figure out how to use an HTTP POST request to submit a captcha. Here is the example code I got from their website:
<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 am supposed to send a post request like that to the web server and get a string returned to me. Here is my attempt to implement that 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;
}
}
Am I supposed to set the value of pict to a File object instead of the String pointing to where the captcha is stored? (captcha.png is the name of the image I am trying to submit).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个更高级别的机制来发送该文件,您不需要创建
WebRequestSettings
并设置其单独的值。您应该将该静态 html 托管在某处并执行如下所示的操作。
如果仍有问题,请在 HtmlUnit 错误跟踪器中提交错误报告。
顺便说一句,HtmlUnit 2.8 即将发布,请尝试一下。
There is a higher-level mechanism to send that file, you don't need to create
WebRequestSettings
and set its individual values.You should host that static html somewhere and do something like the below.
If you still have an issue, please submit a bug report in HtmlUnit bug tracker.
BTW, HtmlUnit 2.8 is about to be released, give it a try.
您不应使用
NameValuePair
< /a> 对于它的子类,KeyDataPair< /代码>
。这样您就可以指定要上传的文件。
以下内容应该有效:
内容类型参数是文件的 MIME 类型。由于您上传的是 PNG 文件,因此它应该是
image/png
。You should not use a
NameValuePair
for this but its subclass,KeyDataPair
. This way you can specify a file to upload.The following should work:
The content type parameter is the MIME type of the file. Since you are uploading a PNG file, it should be
image/png
.这是我试图输入的内容:
PNG 文件是否用 UTF-8 编码?这就是我为文件输入指定 KeyDataPair 的方式吗?我认为我要么指定了错误的 contentType,要么指定了错误的 charSet,或者两者都指定了。我应该把它们全部大写吗?
Here's what I was trying to type:
Are PNG files encoded with UTF-8? Is that how I would specify the KeyDataPair for the file input? I think I am either specifying the wrong contentType or the wrong charSet, or both. Am I supposed to put them in all caps?