ANDROID:在 Webview 和 httpclient 之间共享会话 [英] ANDROID : Share session between Webview and httpclient

查看:41
本文介绍了ANDROID:在 Webview 和 httpclient 之间共享会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WebView 实际上有一个记录的会话.但我也使用 httpclient 从网络发送和获取数据.我在网上看到无法获取WebView的内容,所以我需要使用我的httpclient从webservice获取数据.

I have actually a logged session in my WebView. But I use also httpclient to send and get data from the web. I saw on the internet that it's impossible to get the content of a WebView, so I needed to use my httpclient to get data from a webservice.

问题是这个 webservice 使用了 session...而我的 session 在我的 WebView 中,所以 httpclient 没有它,我无法访问 webservice 的内容.

The problem is that this webservice uses sessions... and my session is in my WebView, so the httpclient doesn't have it and I can't access the content of the webservice.

我看到很多关于这个问题的帖子,但我不明白解决方案.

I see many posts about this problem but I didn't understand the solution.

这是我在 onPageStarted 上所做的:

Here is what i did on my onPageStarted :

CookieManager mgr = CookieManager.getInstance();
Log.i( "URL", url );
Log.i("Cookie",mgr.getCookie("mywebsite.com"));
String cookie_string = mgr.getCookie("mywebsite.com");
if(cookie_string.length() > 1) {                    
    Data.instance().getPref().edit().putString("cookie",cookie_string).commit();
}

我看到我有这种东西,所以我希望那些也包括会话:(我删除了号码)

I saw that I have this kind of things, so I hope those include session too: (i remove the number)

__utma=......(number)......; 

__utmc=number;

__utmz=number.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); 

wt3_eid=%number%number; 

wt3_sid=%number

然后我不知道该怎么做才能在我的 httpclient 中设置这个 cookie.我试过了,没有成功:

Then i don't know what to do in order to set this cookie in my httpclient. I try that, with no success :

HttpClient client = new DefaultHttpClient();
BasicCookieStore cookieStore = new BasicCookieStore();
String login_cookie_string = Data.instance().getPref().getString("cookie", "");
String[] cookie_parts = null;
if(login_cookie_string.length()> 0)
{

    //debug_view.setText(login_cookie_string);
    Log.d("COOKIE", login_cookie_string);
    cookie_parts = login_cookie_string.split(";");

    for(int t=0;t < cookie_parts.length;t++)
    {
        String[] cookieContent = cookie_parts[t].split("=");
        Cookie login_cookie = new BasicClientCookie(cookieContent[0],cookieContent[1]);
        ((BasicClientCookie) login_cookie).setDomain("mywebsite.com");
        cookieStore.addCookie(login_cookie);
    }

}
((AbstractHttpClient) client).setCookieStore(cookieStore);

推荐答案

所以,这就是我所做的并且对我有用 -

So , this is what I did and it worked for me -

HttpRequestBase request = new HttpGet(uri);
request.addHeader("Cookie", getCookieFromAppCookieManager(uri.toString()));

现在getCookieFromAppCookieManager的实现如下-
该方法从应用程序 CookieManager 获取给定 URL 的 cookie.应用程序 CookieManager 管理应用程序的 WebView 实例使用的 cookie.

Now the implmentation for the getCookieFromAppCookieManager is as follows -
The method gets the cookies for a given URL from the application CookieManager. The application CookieManager manages the cookies used by an application's WebView instances.

@param url the URL for which the cookies are requested
@return value the cookies as a string, using the format of the 'Cookie' HTTP request header
@throws MalformedURLException


public static String getCookieFromAppCookieManager(String url) throws MalformedURLException {
    CookieManager cookieManager = CookieManager.getInstance();
    if (cookieManager == null)
        return null;
    String rawCookieHeader = null;
    URL parsedURL = new URL(url);

    // Extract Set-Cookie header value from Android app CookieManager for this URL
    rawCookieHeader = cookieManager.getCookie(parsedURL.getHost());
    if (rawCookieHeader == null)
        return null;
    return rawCookieHeader;
}

这篇关于ANDROID:在 Webview 和 httpclient 之间共享会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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