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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:59:53+00:00 2026-06-14T19:59:53+00:00

I am having a strange problem while using Apache HttpClient in an Android app.

  • 0

I am having a strange problem while using Apache HttpClient in an Android app.

My app needs to Login to a website and get some data and then logout.

My current code looks like this:

public class BlueClient {
    private String hostUrl;
    private DefaultHttpClient client;
    private HttpContext localContext;


    public BlueClient(String hostUrl,DefaultHttpClient httpClient,HttpContext localContext) {

        this.hostUrl = hostUrl;
        this.client = httpClient;
        this.localContext = localContext;

    }

    public boolean doLogin(String userName,String password){
        String url = getHostUrl()+"do_login";//loggin will set a session cookie
        HttpPost post = new HttpPost(url);

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username",userName));
            nameValuePairs.add(new BasicNameValuePair("password",password));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(post,localContext);

            //ok now if response is ok then return true

        } catch (Exception e) {

        }
        return false;
    }

    public MyStuff getMyStuff(){
        String url = getHostUrl()+"/getMyStuff/"; //this url requires authentication. the sesion cookie should do that 
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(get,localContext);
            //ok get my stuff from response and return
        } catch (Exception e) {

        }
        return null;
    }
    public boolean doLogout(){
    String url = getHostUrl()+"do_logout";//it clears the cookie so the session is invalidated
    HttpGet get = new HttpGet(url);
    try {
        HttpResponse response = client.execute(get,localContext);
        //ok the cookie is cleared
      }
    } catch (Exception e) {

    }
    return false;
  }
}

And when i call these function i do like this. It works in emulator but not in device

HttpContext context = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
context.setAttribute(ClientContext.COOKIE_STORE,cookieStore);
DefaultHttpClient httpClient = new DefaultHttpClient();

BlueClient myClient = new BlueClient("http://myHost.com/",httpClient,context);

myClient.doLogin("user","pass");
 // it should've printed the cookies set by the server but i get nothing here !
D.log(context.getAttribute(ClientContext.COOKIE_STORE));
// as this is another subsequesnt request it shoud carry the cookies back to the server but as the cookies are not set this function gives me nothig :-(
myClient.getMyStuff();
myClient.doLogout();

Can anyone please shed some light on this. Why its not working in the device?

  • 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-14T19:59:54+00:00Added an answer on June 14, 2026 at 7:59 pm

    Ah I finally found the problem!

    My server runs on CodeIgniter. And CodeIgniter set the expiry date of the session cookie (ci_session) as Netscape cookie draft compliant. And the format is EEE, dd-MMM-yy HH:mm:ss zzz and Default CookiePoicy used by HttpClient is RFC_2109. So when the httpClient tries to parse the cookie data it fails on parsing the expiry date. I had to explicitly set the Cookie Policy and date format.

    So my final code looks like this:

    BasicHttpParams params = new BasicHttpParams();
    String[] dateFormats = {"EEE, dd-MMM-yy HH:mm:ss zzz"};
    params.setParameter(CookieSpecPNames.DATE_PATTERNS,Arrays.asList(dateFormats));
    
    DefaultHttpClient httpClient = new DefaultHttpClient(params);
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.NETSCAPE);//explicitly set the cookie policy
    
    HttpContext context = new BasicHttpContext();
    CookieStore cookieStore = new BasicCookieStore();
    context.setAttribute(ClientContext.COOKIE_STORE,cookieStore);
    
    
    BlueClient myClient = new BlueClient("http://myHost.com/",httpClient,context);
    
    myClient.doLogin("user","pass");
    myClient.getMyStuff();//now i get my stuff !
    myClient.doLogout();
    

    Hope it saves someone’s time 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a strange problem with ZeroMQ, in which some messages get stuck, and
I'm having a strange problem while using NSDictionnary ... I'm trying to retrieve an
I'm having a strange problem while using @font-face on IE9. I converted a TTF
I'm having a strange problem while trying to make a date from some user
I am having a strange problem, maybe something to do with Apache more than
I'm having a strange problem while attempting to transform a blocking socket server into
I am having a strange problem while returning a string. it says can not
i'm having some really strange problem. i wrote a filemanager in PHP with the
I am having a strange problem while making an application in iPhone. The problem
i am having a strange problem, while calling ajax jquery in asp.net.. i am

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.