繁体   English   中英

android 4.4+中httpClient和webview问题之间的共享会话

[英]Sharing session between httpClient and webView issue in android 4.4+

我在http客户端和网络视图之间共享会话,它在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 4.4添加了SSL证书固定。 我首先要验证您的代码在没有SSL的情况下能否正常运行。 然后检查代码是否正确处理了SSL握手,并且您没有在Webview和http客户端之间混合使用证书。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM