Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6568571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:29:44+00:00 2026-05-25T14:29:44+00:00

I would like to preserve a session while connecting to server using HttpGet and

  • 0

I would like to preserve a session while connecting to server using HttpGet and I need to understand how it handles cookies.

The server developer says that he handles all cookies stuff by himself.
I use HttpGet request to access the server as follows:
InputStream isResponse = null;

    HttpGet httpget = new HttpGet(strUrl);
    HttpResponse response = mClient.execute(httpget);

    HttpEntity entity = response.getEntity();
    isResponse = entity.getContent();
    responseBody = convertStreamToString(isResponse);

    return responseBody;
  1. Should I do something more? Does he put the cookie on my device automatically and the HttpGet method knows to use it in order to keep the sessionID using the cookie?

  2. How can I check if the cookie exist on my device, in order to know if the session is “alive”?

  3. In case I use the following code to get the cookie:

    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);
    
    HttpGet httpget = new HttpGet(strUrl);
    HttpResponse response = mClient.execute(httpget,localContext);
    

does the HttpGet still handles the cookies the same as before?

  1. I see that DefaultHttpClient (mClient in the code above) has its own CookieStore. How can I save its cookies and load them next time I create it?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:29:44+00:00Added an answer on May 25, 2026 at 2:29 pm

    So…
    After cracking my head for hours and implementing my own primitive CookieStore, I found Android Asynchronous Http Client implementation, that includes a nice PersistentCookieStore that works great!
    Simply added the jar to my project and used it as follows:

    PersistentCookieStore cookieStore = new PersistentCookieStore(context);
    DefaultHttpClient mClient = new DefaultHttpClient();    
    mClient.setCookieStore(cookieStore);
    

    and that’s it! The cookies are saved and reused when the application is open again.

    Thank you James Smith, where ever you are. You made at least one man happy.

    If anyone interested in my own primitive implementation (that also works) here it is:

    package com.pinhassi.android.utilslib;
    
    import java.util.Date;
    import java.util.List;
    
    import org.apache.http.cookie.Cookie;
    import org.apache.http.impl.client.BasicCookieStore;
    import org.apache.http.impl.cookie.BasicClientCookie;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    
    public class MyPersistentCookieStore extends BasicCookieStore {
    private static final String COOKIES_LIST = "CookiesList";
    private static final String COOKIES_NAMES = "CookiesNames";
    
    private Context mContext;
    /**
     * 
     */
    public MyPersistentCookieStore(Context context) {
        super();
        mContext = context;
        load();
    }
    
    @Override
    public synchronized void clear(){
        super.clear();
        save();
    }
    
    @Override
    public synchronized boolean clearExpired(Date date){
        boolean res = super.clearExpired(date);
        save();
        return res;
    }
    
    @Override
    public synchronized void addCookie(Cookie cookie){
        super.addCookie(cookie);
        save();
    }
    
    @Override
    public synchronized void addCookies(Cookie[] cookie){
        super.addCookies(cookie);
        save();
    }
    
    public synchronized void save()
    {
        Editor editor = mContext.getSharedPreferences(COOKIES_LIST, Context.MODE_PRIVATE).edit();
        editor.clear();
        List <Cookie> cookies = this.getCookies();
        StringBuilder sb = new StringBuilder();
        for (Cookie cookie : cookies)
        {
            editor.putString(cookie.getName(),cookie.getValue());
            sb.append(cookie.getName()+";");
        }
        editor.putString(COOKIES_NAMES,sb.toString());
        editor.commit();
    }
    
    public synchronized void load()
    {
        SharedPreferences prefs = mContext.getSharedPreferences(COOKIES_LIST, Context.MODE_PRIVATE);
        String [] cookies = prefs.getString(COOKIES_NAMES,"").split(";");
        for (String cookieName : cookies){
            String cookieValue = prefs.getString(cookieName, null);
            if (cookieValue!=null)
                super.addCookie(new BasicClientCookie(cookieName,cookieValue));
        }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using
I would like to overload the (/) operator in F# for strings and preserve
I would like to add an element to a list that preserve the order
I'm using acts_as_taggable_on in an app and I would like to extract the tag
I would like to change the text of a HTML element but preserve the
I would like to modify the NavigateUrl property of a Hyperlink control. I need
I have a very pretty DBML diagram that I would like to preserve in
I'm trying to write a highlight plugin, and would like to preserve HTML formatting.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.