问题描述
我居然在我的WebView一个登录会话。但我用同样的HttpClient从网络上发送和获取数据。我在网上看到,这是不可能得到的WebView的内容,所以我需要用我的HttpClient从Web服务获取数据。
现在的问题是,这个Web服务使用会话......而我会在我的WebView,所以HttpClient的没有它,我不能访问web服务的内容。
我看到有关这个问题的很多帖子,但我不明白的解决方案。
下面是我做我的onPageStarted:
CookieManager经理= CookieManager.getInstance();
Log.i(URL,网址);
Log.i(曲奇,mgr.getCookie(mywebsite.com));
串cookie_string = mgr.getCookie(mywebsite.com);
如果(cookie_string.length()→1){
。Data.instance()获得preF()编辑()putString(曲奇,cookie_string).commit()。
}
我看到,我有这样的事情,所以我希望那些有会议太:(我删除的数量)
__ UTMA = ......(数量)......;
__utmc =号;
__utmz = number.utmcsr =(直接)| utmccn =(直接)| utmcmd =(无);
wt3_eid =%人数%人数;
wt3_sid =%数量
话,我不知道该怎么做才能在我的HttpClient设置这个cookie。我尝试,但没有成功:
HttpClient的客户端=新DefaultHttpClient();
BasicCookieStore的CookieStore =新BasicCookieStore();
字符串login_cookie_string = Data.instance()得到preF()的getString(曲奇,);
的String [] cookie_parts = NULL;
如果(login_cookie_string.length()大于0)
{
//debug_view.setText(login_cookie_string);
Log.d(曲奇,login_cookie_string);
cookie_parts = login_cookie_string.split(;);
对于(INT T = 0; T< cookie_parts.length;吨++)
{
的String [] cookieContent = cookie_parts [T] .split(=);
饼干login_cookie =新BasicClientCookie(cookieContent [0],cookieContent [1]);
((BasicClientCookie)login_cookie).setDomain(mywebsite.com);
cookieStore.addCookie(login_cookie);
}
}
((AbstractHttpClient)客户端).setCookieStore(CookieStore的);
所以,这是我做过什么和它的工作对我来说 -
的Htt prequestBase请求=新HTTPGET(URI);
request.addHeader(曲奇,getCookieFromAppCookieManager(uri.toString()));
现在的implmentation为getCookieFromAppCookieManager如下 - 中国该方法得到的Cookie从应用程序CookieManager一个给定的URL。应用CookieManager器管理应用程序的web视图实例的cookie。
@参数的URL该Cookie的请求的URL
@返回值的cookie作为一个字符串,使用曲奇HTTP请求头的格式
@throws MalformedURLException的
公共静态字符串getCookieFromAppCookieManager(字符串URL)抛出MalformedURLException的{
CookieManager cookieManager = CookieManager.getInstance();
如果(cookieManager == NULL)
返回null;
字符串rawCookieHeader = NULL;
URL parsedURL =新的网址(URL);
从Android应用程序CookieManager此URL //提取设置Cookie标头值
rawCookieHeader = cookieManager.getCookie(parsedURL.getHost());
如果(rawCookieHeader == NULL)
返回null;
返回rawCookieHeader;
}
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.
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.
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
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()));
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:web视图之间共享会话的HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!