I have an Android login form that I want to use to send an HttpPost request to the server and get the cookie back if the login was successful. I have one problem, how can I implement the correct version of this and how can I get a cookie and save it on the device (store it in the preferences database so that I can destroy it later?).
I have this code right now:
public void postData() { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://a_site.com/logintest.aspx"); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("txtUsername", "username")); nameValuePairs.add(new BasicNameValuePair("txtPassword", "123456")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); Log.v(TAG, "Response from server: " + response.toString()); } catch (ClientProtocolException e) { } catch (IOException e) { } }
How to get a cookie and how to save it if the login was successful?
source share