I'm trying to login to a website using HttpPost. My goal is to login and get the necessary cookies that are provided by the website. So far I've been successful at "logging" in but the problem persists with redirection. After I login, I receive success message from the server but in reality, website redirects to a different url and there provides the cookies I need for the session. I've checked how the website "works" with chrome developer tools and I have the necessary parameters in order to do post.
My client at the moment isn't receiving the needed cookies, as it isn't following up with the redirect. I'd like to know what to do next basically. How can I follow up with the server redirects after logging in, in order to get the needed cookies? What classes/methods should I use? I read about htmlUnit but I'd like to avoid it, because the library/data size is huge and my application is only 0.5mb.
What I'm doing now (simplified):
Connect -> Login (httpPost) -> Success (200) -> I receive only one cookie (which isnt the one I need).
What I think I should be doing (simplified):
Connect -> Login -> Success (200) -> Follow up with the redirect -> Get the cookies (total of 5)
.
Thank you for your time and here's the code. The cookie that I'm using at the moment is the incorrect one.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = null;
Log.d("LOGGING" , "Posting...");
HttpPost httpost = new HttpPost(url);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("user", "myUser"));
nvps.add(new BasicNameValuePair("passwrd", "myPw"));
nvps.add(new BasicNameValuePair("openid_identifier", ""));
nvps.add(new BasicNameValuePair("cookielength", "-1"));
nvps.add(new BasicNameValuePair("hash_passwrd", ""));
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Log.d("LOGGING" , "Getting login response...");
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
Log.d("LOGGING" , "Login form getStatus: " + response.getStatusLine());
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
Log.d("LOGGING" , "Cookie: " + cookies.toString());
String SESSION_NAME = "PHPSESSID";
String[] sessionValue = cookies.toString().split("value: ");
String[] sessionValue1 = sessionValue[1].split("]");
String realSessionValue = sessionValue1[0].toString();
Log.d("LOGGING", "SESSION NAME:" + realSessionValue);
/*
Connection.Response loginForm = Jsoup.connect(url)
.method(Connection.Method.GET)
.referrer(referrer)
.execute();
Document login = Jsoup.connect(url)
.data("user", "myUser")
.data("passwrd", "myPw")
.data("cookielength", "1440")
.data("openid_identifier", "")
.data("hash_password", "")
.referrer(referrer)
.cookie(SESSION_NAME, realSessionValue)
.post();
*/
doc = Jsoup.connect(url)
.cookie(SESSION_NAME, realSessionValue)
.get();
//String body = doc.select("body").html();
//Log.d("LOGGING", "Body: " + body.toString());
//Check login
Elements bodies = doc.select("body").first().select("div#main-container-radio").first().select("div#radiorow").first().select("div.bghue").first().select("form#shoutbox-form-r");
Log.d("LOGGING", "Login status: " + bodies.toString());