При отправке постом того же, что отправляет браузер, ругается, что «Доступ запрещен. Причина: Неправильный код защиты CSRF. Возможно сессия устарела». Сначала получаю этот csrf get-ом, потом отправляю логин, пароль и его постом. Попробовал добавить кукис с ним же, но у браузера то, что отправлено и то, что в cookies, отличается. Наверное, его надо как-то иначе получать. Не понимаю.
HttpGet getChecksum = new HttpGet("http://www.linux.org.ru/login.jsp");
BasicCookieStore cookieStore = new BasicCookieStore();
BasicHttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpResponse response = lorClient.execute(getChecksum,localContext);
StringBuilder body = inputStreamToString(response.getEntity().getContent());
String start = "name=\"csrf\" value=\"";
String end = "\"> <label>Имя:";
String csrf = body.substring(body.lastIndexOf(start) + start.length(), body.lastIndexOf(end));
HttpPost request = new HttpPost("http://www.linux.org.ru/login.jsp");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("nick", userName));
nameValuePairs.add(new BasicNameValuePair("passwd", password));
nameValuePairs.add(new BasicNameValuePair("csrf", csrf));
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
BasicClientCookie csrfCookie = new BasicClientCookie("CSRF_TOKEN", csrf);
cookieStore.addCookie(csrfCookie);
HttpResponse response2 = lorClient.execute(request);
Toast.makeText(this, response2.getStatusLine().toString(), Toast.LENGTH_LONG).show();