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(); 


 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。