java - 从 Java 填写 Google 表单

标签 java google-forms autofill

我需要从我的 Java 代码填写基本的 Google 表单,但它抛出org.apache.http.client.ClientProtocolException:意外的响应状态: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;
    }

这是表格:

enter image description here

我做错了什么?

最佳答案

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

  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;
    }
    

查看自述文件和单元测试以获取更多详细信息和示例:

https://github.com/stepio/jgforms

关于java - 从 Java 填写 Google 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59753981/

相关文章:

html - 自动完成 = "new password"不工作, "use password for"下拉列表仍然显示

java - 在 Java 中将文件值读取到文本区域

google-apps-script - 无论如何强制谷歌表单总是RTL?

javascript - 在谷歌应用程序中获取最新表单提交的 responseID

google-api - 是否有可供 3rd 方产品使用的 Google Forms API?

excel - 我想选择动态单元格并自动填充到最后一行

VBA使用最后一个单元格根据相邻列自动填充

java - 返回所有 HtmlPage 的 HTML

java - 带有 java.lang.IllegalStateException : Cannot forward after response has been committed 的 Servlet

java - 如何在 Android java 插件端等待异步操作(任何 I/O)?