在发布帖子请求时无法从jsoup获得结果

时间:2015-06-19 16:59:13

标签: java c# .net jsoup scrape

这是代码段,它总是返回错误页面

    try {
        String url = "http://kepler.sos.ca.gov/";
        Connection.Response response = Jsoup.connect(url)
                .method(Connection.Method.GET)
                .execute();

        Document responseDocument = response.parse();

        Element eventValidation = responseDocument.select("input[name=__EVENTVALIDATION]").first();
        Element viewState = responseDocument.select("input[name=__VIEWSTATE]").first();
        response = Jsoup.connect(url)
                .data("__VIEWSTATE", viewState.attr("value"))
                .data("__EVENTVALIDATION", eventValidation.attr("value"))
                .data("ctl00_content_placeholder_body_BusinessSearch1_TextBox_NameSearch", "escrow")  // <- search 
                .data("ctl00_content_placeholder_body_BusinessSearch1_RadioButtonList_SearchType", "Corporation Name")
                .data("ctl00_content_placeholder_body_BusinessSearch1_Button_Search", "Search")

                .method(Connection.Method.POST)
                .followRedirects(true)
                .execute();
        Document document = response.parse(); //search results
        System.out.println(document);

    } catch (IOException e) {
        e.printStackTrace();
    }

我收到了来自firebug网络面板的请求回复并发送了相同的请求。 我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

根据您的Android版本,该代码将提供&#34; NetworkOnMainThreadExcpetion&#34;如果你试图直接从按钮点击或类似的东西运行它。在蜂窝或更高版本中,您必须从单独的显式线程或AsyncTask进行网络访问。

从我的调试中,您需要添加一些cookie。这包括在下面。此外,您的一些表单字段缺少美元符号,并且传递了一些空白表单字段,这些字段为空但服务器可能期望,所以我也包含了这些字段。

如果您未使用它,我建议使用工具Fiddler来调试此类问题。

class DownloadFilesTask extends AsyncTask<Void, Integer, Long> {
    protected Long doInBackground(Void... params) {
        long totalSize = 0;

        try {
            String url = "http://kepler.sos.ca.gov/";
            Connection.Response response = Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    .execute();

            Document responseDocument = response.parse();
            Map<String, String> loginCookies = response.cookies();


            Element eventValidation = responseDocument.select("input[name=__EVENTVALIDATION]").first();
            String validationKey = eventValidation.attr("value");

            Element viewState = responseDocument.select("input[name=__VIEWSTATE]").first();
            String viewStateKey = viewState.attr("value");

            response = Jsoup.connect(url)
                    .cookies(loginCookies)
                    .data("__EVENTTARGET", "")
                    .data("__EVENTARGUMENT", "")
                    .data("__LASTFOCUS", "")
                    .data("__VIEWSTATE", viewStateKey)
                    .data("__VIEWSTATEENCRYPTED", "")
                    .data("__EVENTVALIDATION", validationKey)
                    .data("ctl00$content_placeholder_body$BusinessSearch1$TextBox_NameSearch", "aaa")  // <- search
                    .data("ctl00$content_placeholder_body$BusinessSearch1$RadioButtonList_SearchType", "Corporation Name")
                    .data("ctl00$content_placeholder_body$BusinessSearch1$Button_Search", "Search")

                    .method(Connection.Method.POST)
                    .followRedirects(true)
                    .execute();
            Document document = response.parse(); //search results
            System.out.println(document);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return totalSize;
    }

    protected void onProgressUpdate(Integer... progress) {
    }

    protected void onPostExecute(Long result) {
    }
}

您实际上会使用以下内容执行该代码:

TestAsyncTask t = new TestAsyncTask();
t.execute();

要获得第2页,您必须包含以下标题。这是伪代码,显然,您必须将其转换为.data调用:

__EVENTTARGET = ctl00$content_placeholder_body$SearchResults1$GridView_SearchResults_Corp
__EVENTARGUMENT = Page$2

你仍然需要其他标题(__VIEWSTATEENCRYPTED空白,如上所述__VIEWSTATE)和上面的cookie。