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();
时间: 2024-10-06 19:52:50