Android同步饼干的WebView和HttpClient的 [英] Android sync cookies webview and httpclient

查看:213
本文介绍了Android同步饼干的WebView和HttpClient的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录的WebView和HttpClient的需要确认用户是否​​登录。 问题是,web视图和HttpClient的使用等饼干这样的HttpClient无法获得的WebView饼干。

I have a login webview and httpclient that need to confirm if the user is logged in. The problem is that the webview and the httpclient are using other cookies so the httpclient can't get the webview cookies.

我看了很多人的问题和教程,但是毫无效果。有些东西我读:

I read a lot of people questions and tutorials, but nothing worked. some of the things I read:

  • <一个href="http://stackoverflow.com/questions/11224454/android-share-session-between-webview-and-httpclient">ANDROID :web视图之间共享会话和HttpClient的
  • <一个href="http://stackoverflow.com/questions/9795934/http-session-synchronization-between-webview-and-java-http-client-in-android">Http在Android的
  • 的WebView和Java HTTP客户端之间的会话同步
  • 如何从拿到饼干HttpClient的?
  • ANDROID : Share session between Webview and httpclient
  • Http session synchronization between webview and java http client in Android
  • How can I get the cookies from HttpClient?

我看在Android上开发一些其他的教程和其他网站,但毫无效果。

I read few other tutorials on Android Development and other websites but nothing worked.

另一篇文章:<一个href="http://stackoverflow.com/questions/28052461/syncing-webview-with-httpclient">http://stackoverflow.com/questions/28052461/syncing-webview-with-httpclient

的问题是,cookies不同步。

这是我尝试过:

        WebView webview;
        webview = (WebView) rootView.findViewById(R.id.webview);

        webview.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                CookieSyncManager.getInstance().sync();

            }
        });
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.setWebChromeClient(new WebChromeClient());
        webview.loadUrl("http://www.klh-dev.com/lehava/lehava/system/mlogin.php");

和一些:

   public String IsLoggedIn() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    HttpClient client=new DefaultHttpClient();



                    HttpGet get=new HttpGet(url);
                     ResponseHandler<String> responseHandler = new BasicResponseHandler();
                    try {
                        response_str=client.execute(get,responseHandler);
                        System.out.println(response_str);
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    Cookie sessionInfo;
                    List<Cookie> cookies = client.getCookieStore().getCookies();

                    if (! cookies.isEmpty()){
                            CookieSyncManager.createInstance(getApplicationContext());
                            CookieManager cookieManager = CookieManager.getInstance();

                            for(Cookie cookie : cookies){
                                    sessionInfo = cookie;
                                    String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
                                    cookieManager.setCookie(URLn, cookieString);
                                    CookieSyncManager.getInstance().sync();
                            }
                    }
                }
            }).start();
            return response_str;
   }

*的HTTPGET返回1或0

我要带饼干从web视图,并利用它们在我的HttpClient的请求

修改(添加darpan的答案):

    public static BasicCookieStore getCookieStore(String cookies, String domain) {
        String[] cookieValues = cookies.split(";");
        BasicCookieStore cs = new BasicCookieStore();

        BasicClientCookie cookie;
        for (int i = 0; i < cookieValues.length; i++) {
            String[] split = cookieValues[i].split("=");
            if (split.length == 2)
                cookie = new BasicClientCookie(split[0], split[1]);
            else
                cookie = new BasicClientCookie(split[0], null);

            cookie.setDomain(domain);
            cs.addCookie(cookie);
        }
        return cs;

        }

    public String IsLoggedIn() {
        new Thread(new Runnable() {
            @Override
            public void run() {




                     String cookies = CookieManager.getInstance().getCookie("http://klh-dev.com/lehava/lehava/system/isloggedin.php");
                     BasicCookieStore lCS = getCookieStore(cookies, "klh-dev.com");

                     HttpContext localContext = new BasicHttpContext();
                     DefaultHttpClient httpclient = new DefaultHttpClient();
                     httpclient.setCookieStore(lCS);
                     localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);

                HttpGet get=new HttpGet("http://klh-dev.com/lehava/lehava/system/isloggedin.php");
                 ResponseHandler<String> responseHandler = new BasicResponseHandler();



                try {
                    result=httpclient.execute(get,localContext);
                    response_str = EntityUtils.toString(result.getEntity());
                    System.out.println(response_str);
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }).start();
        return response_str;
    }
}

EDIT2:最后作品!! 这是我的code:

Finally works!! This is my code:

public static String IsLoggedIn() {
    new Thread(new Runnable() {
        @Override
        public void run() {
                 String cookies = CookieManager.getInstance().getCookie(getUrl);
                 BasicCookieStore lCS = getCookieStore(cookies, "klh-dev.com");

                 HttpContext localContext = new BasicHttpContext();
                 DefaultHttpClient httpclient = new DefaultHttpClient();
                 httpclient.setCookieStore(lCS);
                 localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);

            HttpGet get = new HttpGet("http://klh-dev.com/lehava/lehava/system/isloggedin.php");


            try {
                result=httpclient.execute(get,localContext);
                response_str = EntityUtils.toString(result.getEntity());
                System.out.println(response_str);
                ((MainActivity) getContext).UpdateMenu();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }).start();
    return response_str;
}

和有关的getURL变量。 我不得不这样设置一个全局变量:

and about the getUrl variable. I had to set a global variable like this:

private static String getUrl;

public String getUrl() {
    return getUrl;
}  

在每一个片段,我不得不添加onPageFinished:的getURL = view.getUrl();

On every fragment I had to add onPageFinished: getUrl = view.getUrl();

感谢你。

推荐答案

在你的code,你似乎在做什么,你希望做(获取饼干从 web视图并设置的HttpClient

In your code, you seem to be doing opposite of what you wish to do (Get cookie from Webview and set in HttpClient)

编辑:从铁道部Haviv的的答案 -

您需要定义存储当前的URL视图中的全局变量 -

You need to Define a global variable that stores current url in the view -

private static String getUrl;

从web视图获取饼干 -

Getting cookie from webview -

@Override
public void onPageFinished(WebView view, String url){
    getUrl = view.getUrl();
    String cookies = CookieManager.getInstance().getCookie(getUrl);
    Log.d(TAG, "All the cookies in a string:" + cookies);
}

参考上面的code

在设置这个cookie字符串'你的的HttpClient -

Set this 'cookie string' in your HttpClient -

public static BasicCookieStore getCookieStore(String cookies, String domain) {
String[] cookieValues = cookies.split(";");
BasicCookieStore cs = new BasicCookieStore();

BasicClientCookie cookie;
for (int i = 0; i < cookieValues.length; i++) {
    String[] split = cookieValues[i].split("=");
    if (split.length == 2)
        cookie = new BasicClientCookie(split[0], split[1]);
    else
        cookie = new BasicClientCookie(split[0], null);

    cookie.setDomain(domain);
    cs.addCookie(cookie);
}
return cs;

}
 //And, use the same 'getUrl' here to fetch the cookie. (Haviv's addition)
 String cookies = CookieManager.getInstance().getCookie(getUrl);
 BasicCookieStore lCS = getCookieStore(cookies, YOUR_APP_DOMAIN);

 HttpContext localContext = new BasicHttpContext();
 DefaultHttpClient httpclient = new DefaultHttpClient();
 httpclient.setCookieStore(lCS);
 localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);

PS - 把你的应用程序的域为'YOUR_APP_DOMAIN 而且,我不能检查code,因为我没有你的域名。

PS - Put your app's domain at 'YOUR_APP_DOMAIN' And, I couldn't check the code as I don't have your domain.

参考上面的code

这篇关于Android同步饼干的WebView和HttpClient的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆