posting web pages in android using code

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

posting web pages in android using code

Postby anurahim » Wed Apr 22, 2009 4:21 pm

Hi, I have a login oage which i hve to post with soem user details like user name , passwords, and some hidden values. i need to submit the response page aftr login (its an I agree) to make an API request . I want to do all thse things through code which needs to consider evry aspcts as we are doing the same using browser.
I submited the first login page using code and got the resonse page. on submiting the I agree page I m redirected to a some other page.it seems the cookie is not handled while submiting the second page or may be some redirects or so.

the same is working well if i make the request through browser

the code I used is as follows


try{
DefaultHttpClient httpclient = new DefaultHttpClient();


httpclient.getParams().setParameter(
ClientPNames.COOKIE_POLICY,
CookiePolicy.BROWSER_COMPATIBILITY);
List<Header> headers=new ArrayList<Header>();
headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows; U; Android 1.0;Windows NT 5.1; nl; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13"));
httpclient.getParams().setParameter(ClientPNames.DEFAULT_HEADERS, headers);

CookieSpecRegistry rw = httpclient.getCookieSpecs();

httpclient.getParams().setParameter("http.useragent", "Mozilla/5.0 (Windows; U; Android 1.0; en-gb; generic) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2");

HttpGet httpget = new HttpGet("https://the url to get the login page ");
HttpResponse response = httpclient.execute(httpget);
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

// Log.d("httpPost", "Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}

CookieStore cookies = httpclient.getCookieStore();
System.out.println("cookies:"+cookies.getCookies());


HttpPost loginpostmethod = new HttpPost("https://the url to post the first login page");

ArrayList<BasicNameValuePair> loginnvpairs = new ArrayList<BasicNameValuePair>();
loginnvpairs.add(new BasicNameValuePair("userid",user_id));
loginnvpairs.add(new BasicNameValuePair("pass", password));
loginnvpairs.add(new BasicNameValuePair("sessid", user_sessionid));

loginnvpairs.add(new BasicNameValuePair("hiddenname1", "val1"));
loginnvpairs.add(new BasicNameValuePair("hiddenname2", "val2"));
loginnvpairs.add(new BasicNameValuePair("hiddenname3", "val3"));
loginnvpairs.add(new BasicNameValuePair("hiddenname4", "val4"));


// httpclient.setCookieStore(cookies);
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(loginnvpairs);
loginpostmethod.setEntity(new UrlEncodedFormEntity(loginnvpairs, HTTP.UTF_8));
loginpostmethod.setEntity(p_entity);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(loginpostmethod, responseHandler);
System.out.println(responseBody);
HttpResponse loginpostresponse = httpclient.execute(loginpostmethod);
// httpclient.execute(loginpostmethod, responseHandler);

entity = response.getEntity();
if (entity != null) {
entity.consumeContent();
}

httpclient.setCookieStore(cookies); // cookies from login page
// System.out.println(responseBody);// response for the first LOgin page, the I Agree Page
// get cookies from the I agree Page
CookieStore cookies1 = httpclient.getCookieStore();
System.out.println("cookies1:"+cookies1.getCookies());

/*Iterator it = cookieslist.iterator();

//System.out.println("cookies1 after:"+cookies1.getCookies());
// SUBMITING THE I AGREE PAGE
Log.d("httpPost", "SUBMITING I AGREE PAGE");
//sandbox
HttpPost IAgreePostmethod = new HttpPost("https://url to post the I Agree page");// Posting I agree


ArrayList<BasicNameValuePair> IAgreenvpairs = new ArrayList<BasicNameValuePair>();
IAgreenvpairs.add(new BasicNameValuePair("hidden1", "val1"));
IAgreenvpairs.add(new BasicNameValuePair("hidden2", "val2"));



httpclient.setCookieStore(cookies); // cookies from login page

UrlEncodedFormEntity p_entityIAgree = new UrlEncodedFormEntity(IAgreenvpairs);
IAgreePostmethod.setEntity(new UrlEncodedFormEntity(IAgreenvpairs, HTTP.UTF_8));
IAgreePostmethod.setEntity(p_entityIAgree);
ResponseHandler<String> IAgreeresponseHandler = new BasicResponseHandler();
try{
// String IAgreeSubmitresponseBody = httpclient.execute(IAgreePostmethod, IAgreeresponseHandler);
HttpResponse IAgreeSubmitresponse = httpclient.execute(IAgreePostmethod);
System.out.println("RESPONSE BODY---IAgreePostmethod, IAgreeresponseHandler");
// System.out.println(IAgreeSubmitresponseBody);
System.out.println("SuBMIT RESPONSE---IAgreePostmethod");
System.out.println(IAgreeSubmitresponse.toString());
}catch(Exception e) {
System.err.println("httpclientxml.executeMethod(postxml)"+e);
}
// String IAgreeSubmitresponseBody = httpclient.execute(IAgreePostmethod, IAgreeresponseHandler);
// httpclient.execute(IAgreePostmethod, IAgreeresponseHandler);
// HttpResponse IAgreeSubmitresponse = httpclient.execute(IAgreePostmethod);
//System.out.println(IAgreeSubmitresponseBody);
System.out.println(IAgreeSubmitresponse.toString());

// THIS RESPONSE IS NOT CORRECT AS I EXPECTED.....

}


If any ppl cannot figure out a solution,
can u please tell me whether we can load url in the browser class of android so tht we can get all the data in the browser and can use the methods associated with the browser. if so how can we use it??
anur
anurahim
Junior Developer
Junior Developer
 
Posts: 24
Joined: Fri Jun 27, 2008 11:26 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests