DefaultHttpClient GET and POST commands Java Android
Posted
by RenegadeAndy
on Stack Overflow
See other posts from Stack Overflow
or by RenegadeAndy
Published on 2010-03-24T20:27:50Z
Indexed on
2010/03/25
1:23 UTC
Read the original article
Hit count: 1343
Ok this is my application :
An Android app to allow me to submit CokeZone codes into CokeZone.co.uk from a mobile app instead of from the website.
So I wrote this section of code to do the post logon command and then check to see if im logged in after.
Problem is - the html I get from the homepage after I send the post command is the default - as if im not logged in - so something is going wrong.
Can anyone please help! Its probably the URL im sending the POST to, or the params within the POST command - I havent done much of this stuff so its probably something obvious.
Below is my code so far:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
thisResponse = printPage(entity.getContent());
Log.e("debug",thisResponse);
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
System.out.println("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
HttpPost httpost = new HttpPost("https://secure.cokezone.co.uk/home/blank.jsp?_DARGS=/home/login/login.jsp");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("_dyncharset", "ISO-8859-1"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.loginFormBean.name","renegadeandy%40gmail.com"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.loginFormBean.name", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.cookiedUser", "false"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.cookiedUser", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.loginFormBean.password", "passwordval"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.loginFormBean.password", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.rememberMe", "yes"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.rememberMe", "false"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aSuccessURL", "http://www.cokezone.co.uk/home/index.jsp"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.aSuccessURL", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aErrorURL", "http://www.cokezone.co.uk/home/index.jsphttps://secure.cokezone.co.uk/home/index.jsp"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aErrorURL", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.explicitLogin", "true"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.explicitLogin", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.fICLogin", "login"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.fICLogin", "+"));
nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.fICLogin", "LOGIN"));
nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.fICLogin", "+"));
nvps.add(new BasicNameValuePair("_DARGS", "/home/login/login.jsp"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
thisResponse = printPage(entity.getContent());
entity.consumeContent();
}
Log.e("debug",thisResponse);
Log.e("debug","done");
httpget = new HttpGet("http://www.cokezone.co.uk/home/index.jsp");
response = httpclient.execute(httpget);
entity = response.getEntity();
TextView points = (TextView)findViewById(R.id.points);
points.setText(getPoints(entity.getContent()).toString());
debug.setText(thisResponse);
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
© Stack Overflow or respective owner