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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:52:17+00:00 2026-06-04T07:52:17+00:00

We have an Android application that requires the user to enter an answer to

  • 0

We have an Android application that requires the user to enter an answer to a Captcha.
The Captcha is generated on our server.
When the replies, it is sent to the server for verifying.

Problem is that since I have to close the HttpURLConnection after the request for the Captcha I then find that the reply is running on a different session on the sever.
Because of this the Captcha check fails since it is session dependant.

Is there a way to keep the connection alive or should I be following a different path?
I know that in the equivalent iPhone application they remain “connected” and thus have the same sessionid.

Edit:

    CookieManager cookieManager = new CookieManager();  
    CookieHandler.setDefault(cookieManager);

    URL urlObj = new URL(urlPath);
    conn = (HttpURLConnection) urlObj.openConnection();

    if (urlPath.toLowerCase().startsWith("https:")) {
        initializeHttpsConnection((HttpsURLConnection) conn);
    }
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Language", "en-US");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", Integer.toString(bodyData.length));
    if (_sessionIdCookie != null) {
        conn.setRequestProperty("Cookie", _sessionIdCookie);
    }
    // Connect
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.connect();
  • 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-04T07:52:17+00:00Added an answer on June 4, 2026 at 7:52 am

    Normally, sessions are not kept based on the http connection itself. That wouldn’t make any sense. Sessions are normally kept alive through a cookie on the client side and session information on the server side.
    What you gotta do is save the cookie(s) you’re receiving and then setting that(those) cookie(s) next time you connect to the server.

    To learn more about how to work with sessions and cookies with the HttpUrlConnection class, read the documentation: http://developer.android.com/reference/java/net/HttpURLConnection.html

    Here a little excerpt to get you started:

    To establish and maintain a potentially long-lived session between
    client and server, HttpURLConnection includes an extensible cookie
    manager. Enable VM-wide cookie management using CookieHandler and
    CookieManager:

    CookieManager cookieManager = new CookieManager();  
    CookieHandler.setDefault(cookieManager);
    

    EDIT:

    For those working with API levels 8 or lower, you’ll need to use Apache’s library!

    Here’s some reference code:

     // Create a local instance of cookie store
        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("http://www.google.com/"); 
    
        System.out.println("executing request " + httpget.getURI());
    
        // Pass local context as a parameter
        HttpResponse response = httpclient.execute(httpget, localContext);
    

    The code above was taken from the Apache’s library examples. It can be found here:
    http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java

    EDIT 2:
    Making it clear:

    For the Apache library you need to somehow “connect” the cookie management object with the connection object and you do that through the HttpContext object.

    In HttpUrlConnection’s case that’s not necessary. when you use CookieHandler’s static method setDefault, you’re setting the system-wide cookieHandler. Below is an excerpt from CookieHandler.java. Note the variable name (from the Android Open Source Project’s (AOSP) repository):

     37 /**
     38      * Sets the system-wide cookie handler.
     39      */
     40     public static void setDefault(CookieHandler cHandler) {
     41         systemWideCookieHandler = cHandler;
     42     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this android application that requires to load data from a remote server
I am building an Android application that requires OAuth. I have all the OAuth
I am working on an Android application that requires a user to login before
I'm writing an Android application which requires the user to login. I have a
I have a Android application that requires a splash screen. I have a simple
im making an application on android that requires to capture the user´s voice and
I am writing an Android application that requires the user's current location. I register
I have an android application that requres a login based on users in a
I have an Android application that connects to Facebook to request authorization of an
I have an Android application that uses the Android database to create tables and

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.