【问题标题】:Google Form fill from Java从 Java 填写 Google 表单
【发布时间】:2020-01-15 14:56:50
【问题描述】:

我需要从我的 Java 代码中填写基本的 Google 表单,但它会抛出 org.apache.http.client.ClientProtocolException: Unexpected response status: 405

这是我的代码:

private boolean sendMessage(UserInfo userInfo) {
    final HttpPost req = new HttpPost("my-form-url");
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {

        List<NameValuePair> form = new ArrayList<>();
        form.add(new BasicNameValuePair("entry.1301726507", userInfo.getName()));
        form.add(new BasicNameValuePair("entry.1466759457", "hello"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form, Consts.UTF_8);

        req.setEntity(entity);
        System.out.println("Executing request " + req.getRequestLine());

        ResponseHandler<String> responseHandler = response -> {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity responseEntity = response.getEntity();
                return responseEntity != null ? EntityUtils.toString(responseEntity) : null;
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }
        };
        String responseBody = httpclient.execute(req, responseHandler);
        System.out.println("----------------------------------------");
        System.out.println(responseBody);
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

这是表格:

我做错了什么?

【问题讨论】:

  • 也许没有为此表单定义 Post?这就是错误代码的说明。

标签: java google-forms autofill


【解决方案1】:

您可以使用我的宠物项目为您完成这项工作:

  1. 添加依赖:

    <dependency>
        <groupId>io.github.stepio.jgforms</groupId>
        <artifactId>jgforms</artifactId>
        <version>1.0.1</version>
    </dependency>
    
  2. 定义你的表单:

    public enum MyForm implements MetaData {
    
        USER_NAME(1301726507L),
        MESSAGE(1466759457L);
    
        private long id;
    
        JGForm(long id) {
            this.id = id;
        }
    
        @Override
        public long getId() {
            return this.id;
        }
    }
    
  3. 填写表格并提交:

    private boolean sendMessage(UserInfo userInfo) {
        URL url = Builder.formKey("my-form-key-from-url")
                .put(MyForm.USER_NAME, userInfo.getName())
                .put(MyForm.MESSAGE, "hello")
                .toUrl();
        Submitter submitter = new Submitter(
            new Configuration()
        );
        try {
           submitter.submitForm(url);
        } catch (NotSubmittedException ex) {
            // TODO: log & handle the exception properly
            return false;
        }
        return true;
    }
    

查看 README 和单元测试以获取更多详细信息和示例:

https://github.com/stepio/jgforms

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    相关资源
    最近更新 更多