Let me show using example code (sample code from one of my previous answer here on SO):
public CookieStore sendPostData(String url, String user, String pass) {
// Setup a HTTP client, HttpPost (that contains data you wanna send) and
// a HttpResponse that gonna catch a response.
DefaultHttpClient postClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response;
try {
// Make a List. Increase the size as you wish.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Add your form name and a text that belongs to the actual form.
nameValuePairs.add(new BasicNameValuePair("username_form", user));
nameValuePairs.add(new BasicNameValuePair("password_form", pass));
// Set the entity of your HttpPost.
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute your request against the given url and catch the response.
response = postClient.execute(httpPost);
// Status code 200 == successfully posted data.
if(response.getStatusLine().getStatusCode() == 200) {
// Green light. Catch your cookies from your HTTP response.
CookieStore cookies = postClient.getCookieStore();
return cookies;
}
} catch (Exception e) {
}
}
Now you need to set your cookies (or check/validate them) before doing requests against your server.
Sample code:
CookieStore cookieStore = sendPostData("www.mypage.com/login", "Username",
"Password");
// Note, you may get more than one cookie, therefore this list.
List<Cookie> cookie = cookieStore.getCookies();
// Grab the name of your cookie.
String cookieOne = cookie.get(0).getName();
What you really need to do is to check your HTTP response using an informative tool, such as Wireshark. Login via a computer browser and check/look for the correct values in your response (in Java/Android code you're using String value = cookie.get(0).getValue(); to get a value).
This is how you set cookies for your domain:
// Grab the domain of your cookie.
String cookieOneDomain = cookie.get(0).getDomain();
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(cookieOneDomain, cookieOne);