I'm trying to write a simple Java program to login to a site using HTTPClient library. The problem is I get redirected to the incorrect credentials page. As far as I know, I've captured the cookies and hidden fields and pass them back through POST method. Following is a snippet of the code, I appreciate any comment or hint.
public class ClientFormLogin {
private static final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
BasicCookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();
try {
HttpGet httpget = new HttpGet("http://dubai.dubizzle.com/");
httpget.addHeader("User-Agent", USER_AGENT);
CloseableHttpResponse getResponse = httpclient.execute(httpget);
try {
HttpEntity entity = getResponse.getEntity();
System.out.println("GET: " + getResponse.getStatusLine());
EntityUtils.consume(entity);
List<Cookie> cookies = cookieStore.getCookies();
System.out.println("Get cookies:");
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
Cookie cookie = cookies.get(i);
System.err.println(
"Cookie: " + cookie.getName() +
", Value: " + cookie.getValue() +
", IsPersistent?: " + cookie.isPersistent() +
", Expiry Date: " + cookie.getExpiryDate() +
", Comment: " + cookie.getComment());
}
}
} finally {
getResponse.close();
}
HttpPost httpPost = new HttpPost("http://dubai.dubizzle.com/accounts/login/");
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("userName", "[email protected]"));
urlParameters.add(new BasicNameValuePair("password", "Meta_123"));
urlParameters.add(new BasicNameValuePair("next", "/"));
HttpEntity postParams = new UrlEncodedFormEntity(urlParameters, Consts.UTF_8);
httpPost.setEntity(postParams);
CloseableHttpResponse postResponse = httpclient.execute(httpPost);
System.out.println("POST: " + postResponse.getStatusLine());
List<Cookie> postCookies = cookieStore.getCookies();
if (postCookies.isEmpty()) {
System.out.println("None");
} else {
System.out.println("Post Cookies:");
for (int i = 0; i < postCookies.size(); i++) {
Cookie cookie = postCookies.get(i);
System.err.println(
"Cookie: " + cookie.getName() +
", Value: " + cookie.getValue() +
", IsPersistent?: " + cookie.isPersistent() +
", Expiry Date: " + cookie.getExpiryDate() +
", Comment: " + cookie.getComment());
}
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
postResponse.getEntity().getContent()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
if(!inputLine.matches("^\\s*$")){
response.append(inputLine);
response.append("\n");
}
}
reader.close();
// Print Response Content
System.out.println(response);
} finally {
postResponse.close();
}
} finally {
httpclient.close();
}
}
}
And here is the output showing the cookies:
Jun 21, 2015 2:11:53 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: skybar_sess_False=1; Domain=.dubizzle.com; expires=Mon, 22 Jun 2015 10:11:52 GMT; Path=/". Invalid 'expires' attribute: Mon, 22 Jun 2015 10:11:52 GMT
GET: HTTP/1.1 200 OK
Get cookies:
Cookie: default_site, Value: 2, IsPersistent?: true, Expiry Date: Mon Jun 20 14:11:52 GST 2016, Comment: null
Cookie: sid, Value: 37225e477ebc9230335627fd6dffeb43, IsPersistent?: true, Expiry Date: Sun Jun 28 14:11:52 GST 2015, Comment: null
Jun 21, 2015 2:11:53 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: skybar_sess_False=1; Domain=.dubizzle.com; expires=Mon, 22 Jun 2015 10:11:53 GMT; Path=/". Invalid 'expires' attribute: Mon, 22 Jun 2015 10:11:53 GMT
POST: HTTP/1.1 200 OK
Post Cookies:
Cookie: default_site, Value: 2, IsPersistent?: true, Expiry Date: Mon Jun 20 14:11:53 GST 2016, Comment: null
Cookie: sid, Value: 37225e477ebc9230335627fd6dffeb43, IsPersistent?: true, Expiry Date: Sun Jun 28 14:11:53 GST 2015, Comment: null