Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having some issues with getting myself authenticated with my server.

It works when I manually try to set cookies through a Google plugin like Postman but does not work when it is done through an android device or emulator.

Here is my code for that portion:

String url = "www.thisismyendpoint.com";
String ci_session = "ci_session=token";

CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie stdCookie = new BasicClientCookie("Cookie",ci_session);
cookieStore.addCookie(stdCookie);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();

localContext.setAttribute(ClientContext.COOKIE_STORE,
        cookieStore);
HttpPost httppost = new HttpPost(url);

HttpResponse response = httpClient.execute(httppost, localContext); 

This is my response in the logcat:

02-19 16:43:47.149: D/response(27539): response: <div style="border:1px solid             #990000;padding-left:20px;margin:0 0 10px 0;">
02-19 16:43:47.149: D/response(27539): <h4>A PHP Error was encountered</h4>
02-19 16:43:47.149: D/response(27539): <p>Severity: Notice</p>
02-19 16:43:47.149: D/response(27539): <p>Message: Undefined index: ci_session</p>
02-19 16:43:47.149: D/response(27539): <p>Filename: controllers/test.php</p>
02-19 16:43:47.149: D/response(27539): <p>Line Number: 28</p>
02-19 16:43:47.149: D/response(27539): </div>

Am i doing something wrong? I've tried to set the ci_session into the headers as well but similarly it did not work. Please advice. Thank you!

share|improve this question
It looks like the PHP script has the problem. The message indicates that the "ci_session" not exists in the array that you use to capture the "ci_session" param sended from Java – Lobo Feb 19 at 9:01
but it works when i simulate the same request on the browser so i'm wondering if i failed to do something in android – kenwjj Feb 19 at 9:04

2 Answers

up vote 2 down vote accepted

Are you sure the cookie is set correctly?
In my case, I use HttpGet and set the cookie manually:

HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Cookie", cookie);

The cookie is stored in the sharedPreferences. Should work for HttpPost the same way i think.

share|improve this answer
It works the same way for HttpPost. – Yvan RAJAONARIVONY Feb 19 at 10:09
Yes i got it to work. Can't believe it. the simplest of solutions elude us all the time.. – kenwjj Feb 19 at 11:04

I ran into similar problems. You should not create a new DefaultHttpClient for each new request. Instead, use a static singleton one and use it for all your Http Request needs.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.