简体   繁体   English

如何将图片作为多部分POST请求的一部分发送 - Java HtmlUnit

[英]How to send a picture as part of a multipart POST request - Java HtmlUnit

I am trying to use Java to submit a captcha to decaptcher.com. 我正在尝试使用Java向captaptcher.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. Decaptcher并没有真正解释如何使用他们的API,所以我试图弄清楚如何使用HTTP POST请求来提交验证码。 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. 我应该向Web服务器发送这样的帖子请求并获取一个返回给我的字符串。 Here is my attempt to implement that in Java. 这是我尝试在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? 我应该将pict的值设置为File对象而不是指向验证码存储位置的String吗? (captcha.png is the name of the image I am trying to submit). (captcha.png是我试图提交的图片的名称)。

There is a higher-level mechanism to send that file, you don't need to create WebRequestSettings and set its individual values. 有一个更高级别的机制来发送该文件,您不需要创建WebRequestSettings并设置其各自的值。

You should host that static html somewhere and do something like the below. 你应该在某个地方托管那个静态html并执行类似下面的操作。

If you still have an issue, please submit a bug report in HtmlUnit bug tracker. 如果您仍有问题,请在HtmlUnit错误跟踪器中提交错误报告。

BTW, HtmlUnit 2.8 is about to be released, give it a try. 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();

You should not use a NameValuePair for this but its subclass, KeyDataPair . 你不应该为它使用NameValuePair ,而是它的子类KeyDataPair This way you can specify a file to upload. 这样您就可以指定要上载的文件。

The following should work: 以下应该有效:

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

The content type parameter is the MIME type of the file. 内容类型参数是文件的MIME类型。 Since you are uploading a PNG file, it should be image/png . 由于您要上传PNG文件,因此它应该是image/png

Here's what I was trying to type: 这是我试图输入的内容:

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

Are PNG files encoded with UTF-8? PNG文件是用UTF-8编码的吗? Is that how I would specify the KeyDataPair for the file input? 这是我如何指定文件输入的KeyDataPair? I think I am either specifying the wrong contentType or the wrong charSet, or both. 我想我要么指定错误的contentType或错误的charSet,要么两者都指定。 Am I supposed to put them in all caps? 我应该把它们全部盖上吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM