Отправить POST запрос с сохранением куков
Для авторизации на сайте пользуюсь таким вот кодом:
protected String doInBackground(String... params) {
BufferedReader reader = null;
String l = null;
StringBuffer sb = new StringBuffer("");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.mysite.ru/api/?act=auth");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("login", ""+loginTextVod.getText()));
nameValuePairs.add(new BasicNameValuePair("passwd", ""+passwdTextVod.getText()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "cp1251"), 8);
while ((l = reader.readLine()) != null) {
sb.append(l);
}
reader.close();
return sb.toString();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return null;
}
Авторизируюсь успешно, но если отправлю другой пост-запрос, то сервер, естественно, скажет мне - "гуляй, ты не авторизован". Как сделать, чтоб куки сохранились? В xCode (objective-c) сохраняет само, в Java я еще новичок.
Вот подобный вопрос, но парню так и не помогли:
Источник: Stack Overflow на русском