Android - WebView 设置url的Cookie 免登陆
2016-08-18 17:11
501 查看
说明:
服务器网页要通过网页的Cookie得到客户端的登陆状态,必须客户端给网页Cookie设置值,这样服务端才能得到Cookie的值。步骤:
1、WebView里面必须设置属性:
// 设置可以访问文件myWebView.getSettings().setAllowFileAccess(true);
//如果访问的页面中有Javascript,则webview必须设置支持Javascript
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setUserAgentString(MyApplication.getUserAgent());
myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setAppCacheEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setDatabaseEnabled(true);
2、设置Cookie
/*** Sync Cookie
*/
private void syncCookie(Context context, String url){
try{
Log.d("Nat: webView.syncCookie.url", url);
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
//cookieManager.removeSessionCookie();// 移除
//cookieManager.removeAllCookie();//清除所有cookie
//log
String oldCookie = cookieManager.getCookie(url);
if(oldCookie != null){
Log.d("Nat: webView.syncCookieOutter.oldCookie", oldCookie);
}
//参数 - 通过Builder形式
StringBuilder sbCookie = new StringBuilder();
sbCookie.append(String.format("JSESSIONID=%s","INPUT YOUR JSESSIONID STRING"));
sbCookie.append(String.format(";domain=%s", "INPUT YOUR DOMAIN STRING"));
sbCookie.append(String.format(";path=%s","INPUT YOUR PATH STRING"));
String cookieValue = sbCookie.toString();
cookieManager.setCookie(url, cookieValue);
//参数 - 单独形式
cookieManager.setCookie(url, "JSESSIONID=" + sid);
cookieManager.setCookie(url, "access_token=" + stoken);
//最后一定要调用
CookieSyncManager.getInstance().sync();
//log
String newCookie = cookieManager.getCookie(url);
if(newCookie != null){
Log.d("Nat: webView.syncCookie.newCookie", newCookie);
}
}catch(Exception e){
Log.e("Nat: webView.syncCookie failed", e.toString());
}
}
相关文章推荐
- Unity3d ugui(2) scrollBar的使用
- 安卓仿微信录音功能,带有声音更改
- iOS 常用第三方类库、完整APP示例
- Htc Vive Sdk(OpenVR),Unity3d 开发,UGUI界面响应
- js中的this & var, function & object, prototype & apply
- struts2 Action获取request,session,application
- 浅谈 Swift 柯里化(Currying)
- Android Studio中删除module
- 控件 加阴影
- RecyclerView中添加点击事件
- Android 秒开,对黑白屏说 再见bye bye
- iOS9 适配
- 用RVI方法,对iOS进行抓包
- Android 内存检查
- unity中UI的学习笔记——Text
- Xcode界面编辑器 could not insert new outlet connection
- iOS项目冗余资源扫描脚本
- 通知
- label 字体换色
- iOS 模仿微信发朋友圈表情键盘