how to sync cookies between android.webkit.CookieManager and java.net.HttpUrlConnection on Android

1.6k views Asked by At

I'm making a progam on Android. It has one button and one WebView. If I click the button, It send post message to a web server. (using httpurlconnection) The webview load a page from the same server. I want to sync cookies between core and webview. I found the method from core to webview. But, I couln't find a method from webview to core.

I tried these order.

  1. the page of webview is loaded.
  2. read cookies from webview.
  3. add these cookies to core cookies using HttpCookie.

    URI uri = new URI(url);

    HttpCookie httpCookie = new HttpCookie(cookie.getKey(), cookie.getValue());

    ((java.net.CookieManager) CookieHandler.getDefault()).getCookieStore().add(uri, httpCookie);

but It doesn't working. what is wrong? please advice me about it. Thank you in advance.

1

There are 1 answers

0
Zennichimaro On

you need to set the cookie to your HttpPost object as well, try this:

httpPost.setHeader("Cookie", "PHPSESSID=" + httpCookie.getValue());

where httpPost is an instance of org.apache.http.client.methods.HttpPost