如何在DefaultHttpClient和WebView之间共享cookie

时间:2013-01-20 09:26:10

标签: android cookies webview

如何在DefaultHttpClientWebView之间共享Cookie?

1 个答案:

答案 0 :(得分:0)

用户解决了在DefaultHttpClient和WebView之间共享Cookie的问题。 This solution worked for him,所以他想在那里分享完整的代码。

LoginActivity.java,核心代码是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;
}

在Config.java中:

public static List<Cookie> mCookies = null;

成功登录后,webview浏览:

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); 

网址需要主机,首先他只使用域名(例如stackoverflow.com),但它不起作用。它也必须包括主机(例如,http://stackoverflow.com)。

相关问题