HttpConnection 维护会话
我正在使用一些使用 Rest 架构在 java 中完成的 Web 服务,我的客户端是一个使用 HttpConnection 检索数据的移动应用程序。为了控制身份验证和授权,我使用由 @SessionScoped 类管理的 cookie,但是我不知道如何使会话通过请求持续存在。问题基本上是我将会话管理器注入到 @RequestScoped 的其他服务中,但是由于会话没有持久化,我总是检索 @SessionScoped 类的不同实例,从而删除我之前拥有的所有 cookie 或记录。查看请求标头,我注意到 cookie JSESSIONID,我认为这是由 tomcat 发送的为了持久会话,因此已经尝试在下一个请求中发送相同的 cookie,但是我没有得到任何结果。
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. 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. 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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
评论是正确的...要持久保存会话,您只需在下一个请求中将 JSESSIONID cookie 发送回服务器,这种情况下的问题是 JavaME 中的 HttpConnection 只有 setRequestProperty 方法来包含标头值,现在如果您设置相同的值两次,它会覆盖最后一个。由于我使用的是自定义 cookie 和 JSessionID cookie,因此我按以下方式设置它们:
正确的方法是连接 cookie 字符串,然后将它们全部设置为 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. Since I was using a custom cookie and the JSessionID cookie, I setted them in the following way:
When the correct way to do it is concatenate the cookie strings and then setting a Cookie header with them all: