如何使用Java(jsoup或其他)向Google表单提交数据

时间:2013-12-13 11:49:23

标签: java jsoup

我想将数据从Java提交到Google表单。使用JSoup和以下代码我无法实现这一目标。我已经尝试了几种不同的方法,我搜索了几天仍然没有结果。我的最后一招是在Stackoverflow上欺骗一些聪明人。有人能帮助我吗?

    String url = "http://docs.google.com/forms/d/1OEBmIXG-enn3Yb48JKfRHjHFsTnGUZkIR_7KHR_raF0/viewform";

    try {
        Jsoup.connect(url)
                .data("entry.1278622432", "reply")
                .data("entry.1578309861", "reply")
                //.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                .userAgent("Mozilla")
                .post();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

错误:

org.jsoup.HttpStatusException: HTTP error fetching URL. Status=405, URL=http://doc

Html错误代码:

  

10.4.6 405不允许的方法

     

请求行中指定的方法不允许由Request-URI标识的资源。响应必须包含一个Allow头,其中包含所请求资源的有效方法列表。

(解决方案不一定是JSoup。)

2 个答案:

答案 0 :(得分:2)

您正在视图表单网址上发帖。从浏览器提交此表单时,它的作用是:

curl \
'https://docs.google.com/forms/d/1OEBmIXG-enn3Yb48JKfRHjHFsTnGUZkIR_7KHR_raF0/formResponse' \
-H 'Host: docs.google.com' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
-H 'Accept-Language: nl,en-us;q=0.7,en;q=0.3' -H 'Accept-Encoding: gzip, deflate' -H 'DNT: 1' \
-H 'Referer: https://docs.google.com/forms/d/1OEBmIXG-enn3Yb48JKfRHjHFsTnGUZkIR_7KHR_raF0/viewform' \
-H 'Cookie: GDS_PREF=hl=en_US; __utma=184632636.34...1517515.2; PREF=ID=4922e3....:FF=0:LD=nl:TM=1380883655:LM=1381731571:S=JvyU_OhlkQ7rE3x3; NID=67=hy29...sglz4PVeS53BZ4eLkYK_wDm9-jmdj7apqNZv6rEwUPDobxjagtLN5gpl4A7v0oA' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data 'entry.1278622432=&entry.1578309861=&draftResponse=%5B%5D%0D%0A&pageHistory=0&fbzx=-5804634750901421753'

(感谢FireBug!)这就是你要模仿的东西。从帖子开始到... / formResponse

答案 1 :(得分:0)

添加以下依赖项

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.19.4</version>
    </dependency>

然后导入以下软件包

 import com.sun.jersey.api.client.Client;
 import com.sun.jersey.api.client.ClientResponse;
 import com.sun.jersey.api.client.WebResource;

相应地更改以下代码段

 String url = "https://docs.google.com/forms/d/e/" + FORM_ID + "/formResponse?" + metaData +
            "&draftResponse=%5B%5D%0D%0A&pageHistory=0&fbzx=-5804634750901421753";

 Client client = Client.create();
 WebResource webResource = client.resource(url);
 ClientResponse response =webResource
                    .accept("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                    .header("Host", "docs.google.com")
                    .header("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) " +
                            "Gecko/20100101 Firefox/26.0")
                    .header("Accept-Encoding", " gzip, deflate")
                    .header("Accept-Language", "nl,en-us;q=0.7,en;q=0.3")
                    .header("DNT", 1)
                    .header("Referer", "https://docs.google.com/forms/d/e/" + FORM_ID + "/viewform")
                    .header("Cookie", "GDS_PREF=hl=en_US; __utma=184632636.34...1517515.2; PREF=ID=4922e3....:FF=0:LD=nl:TM=1380883655:LM=1381731571:S=JvyU_OhlkQ7rE3x3; NID=67=hy29...sglz4PVeS53BZ4eLkYK_wDm9-jmdj7apqNZv6rEwUPDobxjagtLN5gpl4A7v0oA")
                    .header("Connection", "keep-alive")
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .header("cookie", "S=spreadsheet_forms=ay7KL1tRTlqur9RqEdI6mxMF6vx3FcsM; CONSENT=YES+LK.en+20180304-09-0; SEARCH_SAMESITE=CgQI5Y4B; HSID=ADSwbqJPF1Qr-Ec-y; SSID=A4q87SuvNBKdb2ple; APISID=gkfURe1KHD3JK_Sw/AEHh8huA9FCjME_AH; SAPISID=BYtObLGnVKKbphJu/AOOdACYgOk7aPfh5h; __Secure-HSID=ADSwbqJPF1Qr-Ec-y; __Secure-SSID=A4q87SuvNBKdb2ple; __Secure-APISID=gkfURe1KHD3JK_Sw/AEHh8huA9FCjME_AH; __Secure-3PAPISID=BYtObLGnVKKbphJu/AOOdACYgOk7aPfh5h; S=billing-ui-v3=qCH3rMDDCaZ_xpidA_rUzezuzbz9IcRd:billing-ui-v3-efe=qCH3rMDDCaZ_xpidA_rUzezuzbz9IcRd:adwords-navi=FttYNTeVNckYg7X8E3ZsxsJ1vM7Zsbb1; SID=tQdHP8MAb80SUf_M7LebR0rky_mob9y0tekyqkfTiK6WCjtIptZqkRG_2zw8vDx7Wyelow.; __Secure-3PSID=tQdHP8MAb80SUf_M7LebR0rky_mob9y0tekyqkfTiK6WCjtIaTSJ2pBkmZ-zBBINW3RhYw.; S=billing-ui-v3=qCH3rMDDCaZ_xpidA_rUzezuzbz9IcRd:billing-ui-v3-efe=qCH3rMDDCaZ_xpidA_rUzezuzbz9IcRd:adwords-navi=FttYNTeVNckYg7X8E3ZsxsJ1vM7Zsbb1:explorer=IK1XkkO0xKJk5bU2F52cs0z1t8mXK5v1; NID=197=rBl9xBCn7YlyxrnqcHF35435sGtnCn6dSss6tr6EPfswg19EFOKGJz_upr5qLgJ922wl0G6ufAnILvoUMx1Csvg9ddVMJE65ALGlSm2u4ERtePDrka2yLQE4XQGvX6WXGNJz9e7HFSmWZTOGD_f0wA2BudviRAbKxqUyPHPxR_uhqAH-andEJDUdosDmuGRg27JKl_IyEd98qlEd-2c_rNx06tE7BnjBkW66v-XB-AGZP0610_JSW2fzemYTPuPC2ZL3iFTFFF4fPP5rhhoNCM2GApidF9yG0hxKFBbTLLphy4IvTl8gBkj10l-DZS_Njr6A8uCts1RfaGL5aq8-yz1IRKEANrheizBWCYNioi1bmgeBl_89WJyYCVfYROBHbPQW0blRimGX4Ql4wcM; 1P_JAR=2020-2-12-4; SIDCC=AN0-TYtB0dfkjw4QetRyEiIaeRH9MuEXLmZe5C8w55MLe8EK_1P0ktruhGOZaXInzu_VfIjkQ8K3")
                    .get(ClientResponse.class);