在android 4.4+中分享httpClient和webView问题之间的会话

时间:2014-06-20 08:15:40

标签: android cookies webview httpclient

我在http客户端和Web视图之间共享会话它在android 4.1或更早版本中对我有用,但它不能在4.4或更高版本中运行?我无法弄清楚原因。非常感谢任何帮助 我的代码是

public class AppSettings extends Application
{
    private static final DefaultHttpClient client = createClient();

    @Override
    public void onCreate()
    {
    }

    public static DefaultHttpClient getClient()
    {
        return client;
    }

    private static DefaultHttpClient createClient()
    {
        BasicHttpParams params = new BasicHttpParams();
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
        schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);
        httpclient.getCookieStore().getCookies();
        return httpclient;
    }
}

在制作http请求时我正在使用下面的代码

try
        {



            DefaultHttpClient mClient = AppSettings.getClient();
            HttpPost httppost = new HttpPost(url);

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));

            //httppost.setEntity(responseBody);

            HttpResponse response = mClient.execute(httppost);

            if (response != null)
            {
                responseBody = EntityUtils.toString(response.getEntity());

            }
            else
            {
                CustomLogger.showLog("Beta", "Response is null");
            }

        }

    catch (Exception e)
    {
        CustomLogger.showLog("Beta", "Exception in Sending data");
        e.printStackTrace();
    }

为了与webView共享我正在使用以下代码

webView.setWebViewClient(new WebViewClient()
                    {

                        @Override
                        public void onPageStarted(WebView view, String url, Bitmap favicon)
                        {
                            super.onPageStarted(view, url, null);
                            CustomLogger.showLog("Beta", "onPage Started url is" + url);
                            webView.setVisibility(View.GONE);
                            progress_loader.setVisibility(View.VISIBLE);
                            DefaultHttpClient mClient = AppSettings.getClient();
                            Cookie sessionInfo;
                            List<Cookie> cookies = mClient.getCookieStore().getCookies();
                            if (!cookies.isEmpty())
                            {
                                CookieSyncManager.createInstance(getActivity());
                                CookieManager cookieManager = CookieManager.getInstance();

                                for (Cookie cookie : cookies)
                                {
                                    sessionInfo = cookie;
                                    String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue()
                                            + "; domain=" + sessionInfo.getDomain();
                                    CustomLogger.showLog("Beta", "cookie string is " + cookieString);
                                    cookieManager.setCookie("http://www.example.com", cookieString);
                                    CookieSyncManager.getInstance().sync();
                                }
                            }
                            else
                            {
                                CustomLogger.showLog("Beta", "No cookies");

                            }
                        }
}

2 个答案:

答案 0 :(得分:0)

如果没有看到更多日志和确切的错误消息,我很难说。 但Android 4.4添加了SSL证书固定。 我首先要验证您的代码在没有SSL的情况下工作正常。然后检查代码是否正确处理SSL握手,以及不在webview和http客户端之间混合证书。

答案 1 :(得分:0)

我已经解决了我的问题。网页视图的OnPageStarted事件被调用了两次,其中我正在检索cookie,请参阅此link。使用简单的计数器,我仅在第一次限制cookie的检索,现在它正常工作