有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试使用Java向decaptcher提交验证码。通用域名格式。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服务器发送一个这样的post请求,并获得返回给我的字符串。下面是我用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的值设置为文件对象,而不是指向captcha存储位置的字符串?(captcha.png是我试图提交的图像的名称)


共 (3) 个答案

  1. # 1 楼答案

    有一种更高级别的机制来发送该文件,您不需要创建WebRequestSettings并设置其各个值

    您应该在某个地方托管静态html,并执行如下操作

    如果您仍然有问题,请在HtmlUnit bug tracker中提交一份bug报告

    顺便说一句,HTMLUnit2.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();
    
  2. # 2 楼答案

    您不应该对此使用^{},而应该使用其子类^{}。这样,您可以指定要上载的文件

    以下方面应起作用:

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

    content type参数是文件的MIME类型。因为您正在上载PNG文件,所以它应该是image/png

  3. # 3 楼答案

    以下是我试图键入的内容:

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

    PNG文件是否使用UTF-8编码?这就是我为文件输入指定KeyDataPair的方式吗?我想我要么指定了错误的contentType,要么指定了错误的字符集,要么两者都指定了。我应该把它们放在所有的帽子里吗