4

How can you share cookies between DefaultHttpClient and WebView?

Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32
Mejonzhan
  • 2,374
  • 1
  • 20
  • 30
  • 3
    Thanks for sharing the solution, but I'd advice to edit this post to comply SO's rules -> make your question sound more like question (i.e. just "how to share cookie..." would do) and then answer your own question). Finally, if noone come with anything better, accept your own answer. – Marcin Orlowski Jan 20 '13 at 09:31
  • maybe you can edit this, and create a proper answer. I think you will get more up-votes that way. – Francisco Corrales Morales May 19 '14 at 15:19
  • I don't see how you link the webView with the Cookies.. How does it knows that it must load the cookies in the request in the webView – Francisco Corrales Morales May 19 '14 at 15:27
  • in fact, CookieSyncManager and CookieManager do this for webview. – Mejonzhan May 22 '14 at 08:07

1 Answers1

0

The user solved sharing Cookies between DefaultHttpClient and WebView. This solution worked for him, so he want to share completely code in there.

LoginActivity.java, the core code is doPost:

private int mNumber = 3;
public InputStream doPost(String url, HashMap<String, String> params,
        String headParam, ArrayList<String> keyValues) {

    DefaultHttpClient httpClient = null;
    InputStream inputStream = null;
    HttpResponse httpResponse = null;
    int statusCode = -1;

    httpClient = (DefaultHttpClient) NetworkManager.getHttpClient();
    HttpPost httpPost = new HttpPost(url);

    if (headParam != null) {
        httpPost.addHeader("Cookie", headParam);
    }

    if (params != null) {

        List<NameValuePair> httpRequestParams = new ArrayList<NameValuePair>();
        Iterator<Entry<String, String>> iter = params.entrySet().iterator();
        while (iter.hasNext()) {

            Map.Entry<String, String> entry = iter.next();
            String key = entry.getKey();
            String val = entry.getValue();
            if (val.equals("multi")) {

                for (String values : keyValues)
                    httpRequestParams.add(new BasicNameValuePair(key,
                            values));
            } else
                httpRequestParams.add(new BasicNameValuePair(key, val));
        }

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(httpRequestParams,
                    HTTP.UTF_8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    try {
        httpResponse = httpClient.execute(httpPost);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        httpPost.abort();
    } catch (IOException e) {
        e.printStackTrace();
        httpPost.abort();
        if (mNumber >= 1) {
            mNumber--;
            doPost(url, params, headParam, keyValues);
            return null;
        }
    }

    if (httpResponse != null) {
        statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode == HttpURLConnection.HTTP_OK) {
            try {
                inputStream = httpResponse.getEntity().getContent();
            } catch (IllegalStateException e) {
                e.printStackTrace();
                httpPost.abort();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            if (mNumber >= 1) {
                mNumber--;
                doPost(url, params, headParam, keyValues);
            } else {
            }
        }
    } else {
        if (mNumber >= 1) {
            mNumber--;
            doPost(url, params, headParam, keyValues);
        }
    }

    Config.mCookies = httpClient.getCookieStore().getCookies();  //save cookies
    return inputStream;
}

In Config.java:

public static List<Cookie> mCookies = null;

After a successful login, webview browse:

List<Cookie> cookies = Config.mCookies;
    if (cookies != null && !cookies.isEmpty()) {

        CookieSyncManager.createInstance(mContext);
        CookieManager cookieManager = CookieManager.getInstance();
        for (Cookie cookie : cookies) {

            Cookie sessionInfo = cookie;
            String cookieString = sessionInfo.getName() + "="
                    + sessionInfo.getValue() + "; domain="
                    + sessionInfo.getDomain();
            cookieManager.setCookie("http://stackoverflow.com", cookieString);
            CookieSyncManager.getInstance().sync();
        }
    }
    mWebview.loadUrl(mLink);
    setCookie(url, string); 

The url needs the host, at first he used just the domain (e.g., stackoverflow.com), but it did not work. It must include the host as well (e.g., http://stackoverflow.com).

Community
  • 1
  • 1
Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32