Reputation: 341
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!
Upvotes: 3
Views: 7404
Reputation: 194
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.
Upvotes: 2
Reputation: 11050
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.
Upvotes: 2