Android HTTP cookie

I am trying to maintain a loggedin user session between my Android application and my Drupal website. In my research, it comes down to sending cookies back to Drupal, but I'm struggling to find a resource that will help me.

Can someone recommend a good tutorial that I can use to perform continuous cookie interaction between Android and Drupal, please?

+1
source share
2 answers

Just in case, someone else had a problem, I had a similar problem, and I was able to solve it using the following code:

1- Define CookieManager and CookieStore in your class

CookieManager cookieManager; CookieStore cookieStore; 

2- Add a default cookie handler, for example. in the class constructor or in the OnCreate method

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

3- Use cookie storage when you execute an HTTP request

 public byte[] openURI(String uri) { try { URI uriObj = new URI(uri); DefaultHttpClient client = new DefaultHttpClient(); // Use the cookieStor with the request if (cookieStore == null) { cookieStore = client.getCookieStore(); } else { client.setCookieStore(cookieStore); } HttpGet getRequest = new HttpGet(uriObj); HttpResponse response = client.execute(getRequest); // Read the response data InputStream instream = response.getEntity().getContent(); int contentLength = (int) response.getEntity().getContentLength(); byte[] data = new byte[contentLength]; instream.read(data); response.getEntity().consumeContent(); return data ; } catch (URISyntaxException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } 
+1
source

I am sure that if you use the HttpClient that comes with the Android API, it will have to manage the session with cookies for you until you close the connection manually.

If I am wrong about this, you can easily get around this by implementing your own cookie store using the CookieStore interface or the BasicCookieStore class. If all else fails, you can store cookies manually and set cookies in the header every time you make an HTTP request.

I'm not sure how this can change for your specific problem, although this will most likely work based on the description of the problem you gave.

0
source

Source: https://habr.com/ru/post/1415682/


All Articles