4

如何在DefaultHttpClient和之间共享 cookie WebView

4

1 回答 1

0

用户解决了在 DefaultHttpClient 和 WebView 之间共享 Cookie。这个解决方案对他有用,所以他想在那里分享完整的代码。

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

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

于 2014-07-02T15:11:44.930 回答