java - unable to get results from jsoup while giving post request -
this code snippet , 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 request response net panel of firebug , sent same. missing something?
depending on android version, code give "networkonmainthreadexcpetion" if try run directly button click or that. on honeycomb or later, have network access separate explicit thread or asynctask.
from debugging, need add cookies. that's included below. also, couple of form fields missing dollar signs, , there blank form fields being passed empty server might expect, included too.
for future reference, recommend tool fiddler debug issues if you're not using 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) { } }
you execute code using like:
testasynctask t = new testasynctask(); t.execute();
to page 2, have include following headers. pseudocode, obviously, you'd have convert .data calls:
__eventtarget = ctl00$content_placeholder_body$searchresults1$gridview_searchresults_corp __eventargument = page$2
and still need other headers ( __viewstateencrypted blank, __viewstate above) , cookies above.
Comments
Post a Comment