【问题标题】:Sharing session between httpClient and webView issue in android 4.4+android 4.4+ 中 httpClient 和 webView 之间的共享会话问题
【发布时间】:2014-08-10 22:48:32
【问题描述】:

我在 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");

                            }
                        }
}

【问题讨论】:

    标签: android cookies webview httpclient


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2012-06-28
        • 1970-01-01
        • 1970-01-01
        • 2017-05-06
        • 2014-10-25
        • 2012-08-24
        • 2011-12-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多