首页 > 代码库 > HttpURLConnection webview sessionId 连贯性
HttpURLConnection webview sessionId 连贯性
GlobalVars.serviceUrl = http://192.168.0.106:8080/text/login.jsp
1 获取 HttpURLConnection 返回的 cookie 和内部的 sessionId
sessionId格式如 JSESSIONID=XXXXXXXXXXXX.tomcat1 不同的服务器SESSION名字是不一样的,
这里我们暂时认为都含有SESSIONID
responseCode = conn.getResponseCode();if (responseCode == HttpURLConnection.HTTP_OK) { InputStream response_in = conn.getInputStream(); GlobalVars.HttpSetCookies = conn.getHeaderFields().get("Set-Cookie"); if(GlobalVars.HttpSessionid==null){ for(String value:GlobalVars.HttpSetCookies){ //默认情况下 SESSION_ID_NAME 必定含有 SESSIONID if(value.toUpperCase().indexOf("SESSIONID")>0){ GlobalVars.HttpSessionid = value; } } }}
2 HttpURLConnection提交时加入 sessionId
url = new URL(str_remotecallservlerurl);conn = (HttpURLConnection)url.openConnection();if(GlobalVars.HttpSessionid !=null ){ conn.setRequestProperty("Cookie",GlobalVars.HttpSessionid);}
3 webview 初始化前 将cookie写入activity的本地即可
CookieSyncManager.createInstance(this); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setCookie(GlobalVars.serviceUrl, GlobalVars.HttpSetCookies.toString()); CookieSyncManager.getInstance().sync();
HttpURLConnection webview sessionId 连贯性
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。