3

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!

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
kenwjj
  • 341
  • 1
  • 7
  • 21
  • 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 '13 at 09: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 '13 at 09:04

2 Answers2

2

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.

Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
2

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.

  • Yes i got it to work. Can't believe it. the simplest of solutions elude us all the time.. – kenwjj Feb 19 '13 at 11:04
  • can you please post your whole solution – Dimitri Nov 14 '13 at 07:21
  • What do you need to understand? `httpGet` is a newly initialied Object, `cookie` is a string you save at a secure location. To call the website with the url (also a string), you have to call `new DefaultHttpClient().execute(httpGet)`, but this is too offtopic in my eyes. – Philipp Kuntschik Nov 15 '13 at 13:09
  • What if there's more than 1 cookie? @PhilippKuntschik – Josh Aug 19 '15 at 12:03