我在android上开发服务器客户端应用程序,我在应用程序的服务器端使用会话,但有时我在服务器上丢失了会话。 Ps:我在服务器上使用https连接。
我正在使用这些来举行会议:
我只使用https证书:
schemeRegistry.register(new Scheme(“https”,sslSocketFactory,443)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params,schemeRegistry);
我在所有http请求后保存我的Cookie:
private void createSessionCookie(){
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (! cookies.isEmpty()){
CookieSyncManager.createInstance(ctx);
CookieManager cookieManager = CookieManager.getInstance();
//sync all the cookies in the httpclient with the webview by generating cookie string
for (Cookie cookie : cookies){
Cookie sessionInfo = cookie;
String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
cookieManager.setCookie(UrlConstants.SERVICE_PRE_URL, cookieString);
CookieSyncManager.getInstance().sync();
}
}
}
即使我正在做这些,我也会失去会话。 请帮我解决这个问题, 谢谢你的建议 最诚挚的问候。
答案 0 :(得分:3)
您不应该手动对cookie执行任何操作,只需在某处创建静态CookieStore
,将其分配给HttpContext
,并在请求中使用该上下文。 Cookie将自动保存和恢复。
这些是你的班级成员:
private static CookieStore cookieStore = new BasicCookieStore();
private HttpClient httpclient = new DefaultHttpClient();
private HttpPost post = new HttpPost("your url here");
这部分进入成员函数,它执行请求:
HttpContext ctx = new BasicHttpContext();
ctx.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpResponse result = httpclient.execute(post,ctx);