简体   繁体   English

HttpConnection维护会话

[英]HttpConnection maintaining Session

I'm consuming some web services done in java using a Rest architecture, my client it's a mobile application that uses HttpConnection to retrieve the data. 我正在使用Rest结构使用Java完成一些Web服务,我的客户端是使用HttpConnection检索数据的移动应用程序。 In order to control authentication and authorization I'm using cookies, managed by a @SessionScoped class, however I don't know how to make the session persist through requests. 为了控制身份验证和授权,我使用由@SessionScoped类管理的cookie,但是我不知道如何使会话通过请求持久化。 The problem basically is that I inject the Session manager in other services that are @RequestScoped, however since the session is not persisted I always retrieve differente instances for the @SessionScoped class, thus deleting all the cookies or records I had before. 问题基本上是,我将会话管理器注入了@RequestScoped的其他服务中,但是由于会话没有持久化,所以我总是为@SessionScoped类检索不同的实例,从而删除了我以前拥有的所有cookie或记录。 Looking at the request headers I noticed the cookie JSESSIONID, I think this is sent by tomcat in order to persist session, so tried already to send the same cookie in the next request, however I got no results. 查看请求标头时,我注意到cookie JSESSIONID,我认为它是由tomcat发送的以便持久化会话,因此已经尝试在下一个请求中发送相同的cookie,但是没有任何结果。

The comments were right... to persist a session you just have to send the JSESSIONID cookie back to server in the next request, the problem in this case was that HttpConnection in JavaME only has the setRequestProperty method to include a header value, now if you set the same value two times it overwrites the last one. 注释是正确的...要保留会话,您只需要在下一个请求中将JSESSIONID cookie发送回服务器即可,这种情况下的问题是JavaME中的HttpConnection仅具有setRequestProperty方法以包含标头值,现在您将相同的值设置为覆盖上一个值的两倍。 Since I was using a custom cookie and the JSessionID cookie, I setted them in the following way: 由于我使用的是自定义cookie和JSessionID cookie,因此我通过以下方式进行设置:

connection.setRequestProperty("Cookie","sessionId="+ApplicationPreferences.getIn‌​stance().getSessionCookieHeader()); 
connection.setRequestProperty("Cookie","JSESSIONID="+ApplicationPreferences‌​.getInstance().getJavaSessionCookieHeader());

When the correct way to do it is concatenate the cookie strings and then setting a Cookie header with them all: 正确的方法是连接cookie字符串,然后使用它们全部设置cookie标头:

String myCookies="sessionId="+ApplicationPreferences.getInstance().getSessionCookieHead‌​er()+";"+"JSESSIONID="+ApplicationPreferences.getInstance().getJavaSessionCookieHeade‌​r(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM