So I am trying to login to a website (in this example myspace) using Apache’s HttpClient v.4 but I am not sure where I am going wrong in the process, when I test this code, the Post Login Cookies are the same as the Initial login cookies, but that shouldn’t be.
I have looked around online to see if anyone else has tried, but I haven’t found a good resource that has worked for me.
I am using this example: apache HttpClient to access facebook ( Facebook)
try{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.myspace.com/auth/form");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
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("http://www.myspace.com/auth/form");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("email", "someEmail"));
nvps.add(new BasicNameValuePair("password", "somePassword"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
//System.out.println("Double check we've got right page " + EntityUtils.toString(entity));
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
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());
}
}
httpclient.getConnectionManager().shutdown();
}
catch(Exception e){e.printStackTrace();}
Example cookie:
[version: 0][name: MSCOUNTRY][value: US][domain: .myspace.com][path: /][expiry: Tue Jun 12 23:22:15 EDT 2012]
[version: 0][name: MSCulture][value: IP=XXX.XXX.XXXX&IPCulture=en-US&PreferredCulture=en-US&PreferredCulturePending=&Country=VVM=&ForcedExpiration=0&timeZone=0&myStuffDma=&myStuffMarket=&USRLOC=RandomUserLocString==&UserFirstVisit=1][domain: .myspace.com][path: /][expiry: Tue Jun 12 23:22:15 EDT 2012]
[version: 0][name: SessionDDF2][value: RandomStringHere==][domain: .myspace.com][path: /][expiry: Sat Jun 05 23:22:15 EDT 2032]
Why do you think that the initial cookies and the post cookies should be different? The application server side assigns a set of cookies to the client and usually that stays the same throughout the session.
The application server associates a set of attributes associated with the session (using cookies as a lookup), and that is what decides whether the session is ‘logged-in’ or not.