rezak90 писал(а):та просто добавляйте куки с каждым следующим запросом. Логика:
1) первый заход на сайт - авторизуемся и получаем куки в requeste;
2) переходим на любую нужную нам страницу и к текущему соединению добавляем куки (что то в виде такого: httpConnection.setCookie(cookie);).
что-то я не пойму
Вот я зашла в первый раз так:
Код: Выделить всё
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
/* returns true if username and password is true*/
HttpPost httppost = new HttpPost(params[0]);
String res = null;
try {
// Add user name and password
EditText uname = (EditText)findViewById(R.id.txt_username);
String username = uname.getText().toString();
EditText pword = (EditText)findViewById(R.id.txt_password);
String password = pword.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("LoginForm[username]", username));
nameValuePairs.add(new BasicNameValuePair("LoginForm[password]", password));
nameValuePairs.add(new BasicNameValuePair("yt0", "Войти"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//System.out.println(httppost);
// Execute HTTP Post Request
Log.w("SENCIDE", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("SENCIDE", str);
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//***********
HttpResponse response1 = httpclient.execute(httppost, localContext);
//***********
List<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
куки есть, и при удачной авторизации я перехожу на другую активити.
Код: Выделить всё
if (res=="Login successful"){
Intent intent = new Intent(MainActivity.this, MainMenu.class);
startActivity(intent);
} else {
tvInfo.setText(res);
}
а дальше как мне быть?
пробую вот так:
Код: Выделить всё
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
/* returns true if username and password is true*/
HttpPost httppost = new HttpPost(params[0]);
String res = null;
try {
// sessionid
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//***********
HttpResponse response1 = httpclient.execute(httppost, localContext);
//***********
List<Cookie> cookies = cookieStore.getCookies();
HttpConnection.setCookie(cookies);
но подчеркивает HttpConnection(HttpConnection cannot be resolved)
Помогите, я что-то сегодня совсем тормоЗЮ
