1

I sharing session between http client and web view it works fine for me in android 4.1 or earlier versions but its not working in 4.4 or above versions? i am not able to figure out the reason.any help will be greatly appreciated My code is

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

While making http request i am using below code

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

in order to share it with webView i am using following code

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

                            }
                        }
}
Rohan
  • 188
  • 4
  • 13

2 Answers2

0

I't hard to tell without seeing some more logs and the exact error message. But Android 4.4 added SSL certificate pinning. I would start by verifying that your code works fine without SSL. and then check that the code properly handle SSL handshake and that you don't mix certificates between the webview and the http client.

ApriOri
  • 2,618
  • 29
  • 48
0

I have solved my problem. OnPageStarted event of web view was getting called twice in which i was retrieving cookies,refer this link.With simple counter ,i restricted the retrieval of cookie only on first occasion and now its working fine

Rohan
  • 188
  • 4
  • 13