I have a login activity and I have to create a post request for my website to login the user into my mobile app. To create post requests on my website I need the csrf cookie as parameter, it means I have first to get the cookie from my URL and after create my post request with the csrf value.
Here is my code:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.178.163:8080/login/");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("username", "xxx"));
nameValuePairs.add(new BasicNameValuePair("password", "yyy"));
//csrfmiddlewaretoken
String res = null;
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
res = response.toString();
res = res.replaceAll("\\s+","");
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
Log.i("line", line);
//System.out.println(line);
if (line.startsWith("csrftoken=")) {
String key = line.substring(5);
Log.i("key", key);
}
}
}
catch (IOException e) {
txt_Error.setText(e.toString());
}
Any idea how to do it? I already read about CookieSyncManager but I didnt understand at all... Any idea or code sample will be aprreciate