简体   繁体   中英

How do I store cookie for subsequent connections with HttpClient

I have the following code that posts login details to a website. it works but how can I have the session cookie so I dont have to log in again for the other pages

edit: updated code

imports...

public class Login extends Activity {
    Button bLogin;
    EditText teUsername, tePassword;
    CheckBox chbRememberPass;

    HttpClient httpclient;
    HttpResponse response;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initialiseVars();
        httpclient = new DefaultHttpClient();

        bLogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                checkLoginDetails();
                test0();
            }
        });
    }

    private void initialiseVars() {
        bLogin = (Button) findViewById(R.id.bLogin);
        teUsername = (EditText) findViewById(R.id.etUsername);
        tePassword = (EditText) findViewById(R.id.etPassword);
        chbRememberPass = (CheckBox) findViewById(R.id.chkRememberPass);
    }

    private void checkLoginDetails() {


        HttpPost httppost = new HttpPost(
                "mywebsite/login.php");
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
        nameValuePairs.add(new BasicNameValuePair("username", "admin"));
        nameValuePairs.add(new BasicNameValuePair("password", "pass"));

        try {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.d("myapp", "works till here. 2");
            try {
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String responseBody = httpclient.execute(httppost, responseHandler);

                    Log.d("firstCon",responseBody);

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    private void test0() {

        HttpGet httppost = new HttpGet(
                "https://mywebsite/userSettings.php");

        try {
            response = httpclient.execute(httppost);
            //String responseBody = EntityUtils.toString(response.getEntity());         
            try {
                Log.d("secondCon", test());

            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("seconderror", e.toString());
            }


        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    }

}

Instead of creating a new DefaultHttpClient , reuse it. Your cookies are stored in DefaultHttpCilent , so if you keep reusing the same instance, your cookies will be automatically handled for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM