我要被用于发送我的Android应用程序会话cookie的服务器 经过身份验证的通信。我试图加载的WebView与URL 指向一个相同的服务器和我试图传递会话 饼干进行身份验证。我观察到它的工作原理 间歇性,但我不知道为什么。我用的是同一个会话cookie来使我的服务器上的其他电话,这些永远不会失败的身份验证。我只是想在一个的WebView加载一个URL时,必须遵守这个问题,它不会每次都发生。非常令人沮丧的。
下面是我使用这样做的code。任何帮助将大大AP preciated。
字符串myUrl =http://mydomain.com/;
CookieSyncManager.createInstance(本);
CookieManager cookieManager = CookieManager.getInstance();
曲奇sessionCookie =的getCookie();
如果(sessionCookie!= NULL){
串cookieString = sessionCookie.getName()+=+ sessionCookie.getValue()+;域=+ sessionCookie.getDomain();
cookieManager.setCookie(myUrl,cookieString);
CookieSyncManager.getInstance()同步()。
}
的WebView web视图=(web视图)findViewById(R.id.webview);
。webView.getSettings()setBuiltInZoomControls(真正的);
webView.getSettings()setJavaScriptEnabled(真)。
webView.setWebViewClient(新MyWebViewClient());
webView.loadUrl(myUrl);
解决方案
由于 justingrammens !这为我工作,我设法在我DefaultHttpClient请求和的WebView活动共享的cookie:
// -------本地请求活动
私人DefaultHttpClient HttpClient的;
公共静态饼干饼干= NULL;
//后登录
名单<饼干>饼干= httpClient.getCookieStore()的getCookies()。
的for(int i = 0; I< cookies.size();我++){
饼干= cookies.get(ⅰ);
}
// ------- Web浏览器活动
曲奇sessionCookie = myapp.cookie;
CookieSyncManager.createInstance(本);
CookieManager cookieManager = CookieManager.getInstance();
如果(sessionCookie!= NULL){
cookieManager.removeSessionCookie();
串cookieString = sessionCookie.getName()+=+ sessionCookie.getValue()+;域=+ sessionCookie.getDomain();
cookieManager.setCookie(myapp.domain,cookieString);
CookieSyncManager.getInstance()同步()。
}
I have a server that sends my android app a session cookie to be used for authenticated communication. I am trying to load a WebView with a URL pointing to that same server and I'm trying to pass in the session cookie for authentication. I am observing that it works intermittently but I have no idea why. I use the same session cookie to make other calls on my server and these never fail authentication. I only observe this problem when trying to load a URL in a WebView, and it does not happen every time. Very frustrating.
Below is the code that I'm using to do this. Any help will be greatly appreciated.
String myUrl = ""http://mydomain.com/";
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
Cookie sessionCookie = getCookie();
if(sessionCookie != null){
String cookieString = sessionCookie.getName() +"="+sessionCookie.getValue()+"; domain="+sessionCookie.getDomain();
cookieManager.setCookie(myUrl, cookieString);
CookieSyncManager.getInstance().sync();
}
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(myUrl);
解决方案
Thanks justingrammens! That worked for me, I managed to share the cookie within my DefaultHttpClient requests and WebView activity:
//------- Native request activity
private DefaultHttpClient httpClient;
public static Cookie cookie = null;
//After Login
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
for (int i = 0; i < cookies.size(); i++) {
cookie = cookies.get(i);
}
//------- Web Browser activity
Cookie sessionCookie = myapp.cookie;
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
if (sessionCookie != null) {
cookieManager.removeSessionCookie();
String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" + sessionCookie.getDomain();
cookieManager.setCookie(myapp.domain, cookieString);
CookieSyncManager.getInstance().sync();
}