无法从jsoup同时给予POST请求到得到结果 [英] unable to get results from jsoup while giving post request
问题描述
这是在code段,它总是返回错误页面
This is the code snippet , it always returns error page
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();
}
我从萤火虫净面板的请求响应,并派出相同。 我失去了一些东西?
I got the request response from net panel of firebug and sent the same. Am I missing something?
推荐答案
根据您的Android版本,即code会给出一个NetworkOnMainThreadExcpetion如果你尝试直接从单击按钮或类似的东西运行。在蜂窝或更高版本,您需要做的网络访问从一个单独的明确的线程或AsyncTask的。
Depending on your android version, that code will give a "NetworkOnMainThreadExcpetion" if you try to run it directly from a button click or something like that. On honeycomb or later, you have to do network access from a separate explicit thread or a AsyncTask.
这是我调试,你需要添加一些饼干。这包括在下面。此外,一对夫妇的表单域是缺少美元符号,并有被传递一些空白表单字段是空的,但服务器所期望的,所以我包括这些呢。
From my debugging, you need to add some cookies. That's included below. Also, a couple of your form fields were missing dollar signs, and there were some blank form fields being passed that were empty but the server might expect, so I included those too.
有关备查,我推荐的工具提琴手调试问题,像这样的,如果你又不是使用它了。
For future reference, I recommend the tool Fiddler to debug issues like this if you're not using it already.
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) {
}
}
使用像您会实际执行的code:
You would actually execute that code using something like:
TestAsyncTask t = new TestAsyncTask();
t.execute();
要获得第2,你必须包括下列头。这是伪code,很明显,你必须将其转换为。数据呼叫:
To get Page 2, you would have to include the following headers. This is pseudocode, obviously, you'd have to convert it to .data calls:
__EVENTTARGET = ctl00$content_placeholder_body$SearchResults1$GridView_SearchResults_Corp
__EVENTARGUMENT = Page$2
和你仍然需要其他标题(__VIEWSTATEENCRYPTED空白,__VIEWSTATE如上)和饼干上面。
And you still need the other headers ( __VIEWSTATEENCRYPTED blank, __VIEWSTATE as above) and cookies as above.
这篇关于无法从jsoup同时给予POST请求到得到结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!