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 8388951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:35:23+00:00 2026-06-09T18:35:23+00:00

I want to save cookies after the post requests. Class for httpClient realization: public

  • 0

I want to save cookies after the post requests. Class for httpClient realization:

 public class JSONParser {
    CookieStore store = new BasicCookieStore();
       static InputStream is = null;
        static JSONObject jObj = null;
        static String json = "";
        private static final DefaultHttpClient httpClient = new DefaultHttpClient() ;
     public static DefaultHttpClient getInstance() { return httpClient; }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            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);

            DefaultHttpClient httpClient = JSONParser.getInstance();
            List<Cookie> cookies = httpClient.getCookieStore().getCookies();
            httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Android-AEApp,ID=2435743");
            httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            httpPost.setHeader("User-Agent","Android-AEApp,ID=2435743");

            cookies = httpClient.getCookieStore().getCookies();
            httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            httpPost.setHeader("Cookie", "PHPSESSID=lc89a2uu0rj6t2p219gc2cq4i2; path=/");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            cookies = httpClient.getCookieStore().getCookies();

            if (cookies.isEmpty()) {
                for (Cookie a : cookies)
                cookieStore.addCookie(a);
            } else {
                for (Cookie a : cookies) {
                    cookieStore.addCookie(a);
                    System.out.println("- " + a.getValue().toString());
                }
            }

            is = httpEntity.getContent();


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

I create singlotone for my DefaultHttpClient and want to use it with further requests. When I try to make like this:

 HttpResponse httpResponse = httpClient.execute(httpPost, localContext);

I get no of cookies. When I do not include localContext, I can get cookies with

 if (cookies.isEmpty()) {
                for (Cookie a : cookies)
                cookieStore.addCookie(a);
            } else {
                for (Cookie a : cookies) {
                    cookieStore.addCookie(a);
                    System.out.println("- " + a.getValue().toString());
                }
            }

But cookies do not setting at second request. I guess – reason is in not using localContext. Tell me what’s the problem and how can I set cookie if they have been changing?

  • 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-06-09T18:35:24+00:00Added an answer on June 9, 2026 at 6:35 pm

    I used Singletone for ArrayList of my Cookies. It looks like this:

    public class CookieStorage {
    
    
        private ArrayList<Object> arrayList;
    
        private static CookieStorage instance;
    
        private CookieStorage(){
            arrayList = new ArrayList<Object>();
        }
    
        public static CookieStorage getInstance(){
            if (instance == null){
                instance = new CookieStorage();
            }
            return instance;
        }
    
        public ArrayList<Object> getArrayList() {
            return arrayList;
        }
    
        @Override
        public String toString()
        {       
        return getArrayList().toString();   
        }
    }
    

    And my HttpClient class:

    public class HttpClient {
    static InputStream is = null;
    private static final DefaultHttpClient httpClient = new DefaultHttpClient() ;
    public static DefaultHttpClient getInstance() { return httpClient; }
    
     public static InputStream getResponse(String url, List<NameValuePair> params)
     {
        try {
        CookieStore cookieStore = new BasicCookieStore();
        DefaultHttpClient httpClient = JSONParser.getInstance();        
        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Android-AEApp,ID=2435743");
        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
        BasicClientCookie cookie = new BasicClientCookie("PHPSESSID", "1");  
        httpClient.setCookieStore(cookieStore);
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        httpPost.setHeader("User-Agent","Android-AEApp,ID=2435743");
        httpPost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        System.out.println(CookieStorage.getInstance().toString());
        if (CookieStorage.getInstance().getArrayList().isEmpty())
            CookieStorage.getInstance().getArrayList().add("PHPSESSID=lc89a2uu0rj6t2p219gc2cq4i2");
        httpPost.setHeader("Cookie", CookieStorage.getInstance().getArrayList().get(0).toString());
        HttpResponse httpResponse = httpClient.execute(httpPost);
        Header[] head = httpResponse.getAllHeaders();
        System.out.println(cookie);
    
        if (httpResponse.getLastHeader("Set-Cookie")!=null)
        {
    
            CookieStorage.getInstance().getArrayList().remove(0);
            CookieStorage.getInstance().getArrayList().add(httpResponse.getLastHeader("Set-Cookie").getValue());
        }
        Log.i("arrayList",(CookieStorage.getInstance().getArrayList().toString()));
    
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    
    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
         return is;
     }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to capture an ajax http request w/ all of its headers/cookies/post params
i want jsp or javascript for save form data for history -no cookies -no
I'm using this code to save cookies: function saveCookie(name,value) { var date = new
How can I port this wget stuff to scala: wget --keep-session-cookies --save-cookies cookies.txt --post-data
I want save the username and password when user login, and I added the
i want to save a variable to be global variable on all screens i
I want to save values as an ArrayList of double in a file. Whenever
I want to save webpage in document directory with the images,css and javascripts etc..
I want to save my program's DataModel objects to a file, and be able
I want to save a local copy of xml being ouput by a certain

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.