简体   繁体   English

无需浏览器即可以编程方式保持HTTP Session Alive

[英]Programmatically keep HTTP Session Alive without browser

For one of our requirements I am talking between two servers using HTTP protocol. 对于我们的一个要求,我在使用HTTP协议的两台服务器之间进行通信。 The interaction is long running, where a user might not interact with the other site for pretty long intervals. 交互是长时间运行的,用户可能不会在很长的时间间隔内与其他站点进行交互。

When they come to the page, the log in into the remote site. 当他们来到页面时,登录到远程站点。 Every time user tried to interact with the remote site, internally I make a HTTP call (authetication is done based on sessionId). 每当用户尝试与远程站点交互时,我在内部进行HTTP调用(基于sessionId进行验证)。

I was wondering if there is a way to also refresh the session and ensure that it does not expire. 我想知道是否有办法刷新会话并确保它不会过期。

As per my limited understanding, browser handles this by passing keep-alive in header or cookie (which I don't understand completely). 根据我的有限理解,浏览器通过在标头或cookie中传递keep-alive来处理这个问题(我完全不了解)。 Can anyone suggest a programmatic way in Java to achieve keep-alive behavior 任何人都可以建议使用Java编程方式来实现保持活动的行为

1. 1。

 <session-config>
         <session-timeout>-1</session-timeout>
 </session-config>

Simply paste this piece if code in your deployment descriptor (DD). 如果代码在部署描述符(DD)中,则只需粘贴此部分。
If you want to keep your session alive for a particular duration of time replace -1 with any positive numeric value. 如果要在特定的持续时间内保持会话处于活动状态,请将-1替换为任何正数值。
Time specified here is in minutes. 此处指定的时间以分钟为单位。

2. 2。

If you want to change session timeout value for a particular session instance without affecting the timeout length of any other session in the application : 如果要更改特定会话实例的会话超时值,而不影响应用程序中任何其他会话的超时长度:

session.setMaxInactiveInterval(30*60);


** * ** * ** * ** * ** * ** * ** * * ** * ** * ** * ** * ** * ** * ** * *
Note : 注意 :

1.In DD, the time specified is in minutes . 1.在DD中,指定的时间以分钟为单位
2.If you do it programatically, the time specified is in seconds . 2.如果以编程方式执行,则指定的时间以单位

Hope this helps :) 希望这可以帮助 :)

I guess below code can help you, if you can pass JSESSIONID cookie then your container will take responsibility to keep the session alive if its same session that might be created from some other browser. 我想下面的代码可以帮助你,如果你可以传递JSESSIONID cookie,那么你的容器将负责保持会话活着,如果它可能是从其他浏览器创建的相同会话。

find the link below that explained a lot. 找到下面的链接,解释了很多。

Click Here 点击这里

Code snippet from the link above 上面链接的代码段

    BasicCookieStore cookieStore = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "97E3D1012B3802114FA0A844EDE7B2ED");
    cookie.setDomain("localhost");
    cookie.setPath("/CookieTest");
    cookieStore.addCookie(cookie);
    HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();

    final HttpGet request = new HttpGet("http://localhost:1234/CookieTest/CookieTester");

    HttpResponse response = client.execute(request);

Take a look of Apache HttpClient and see its tutorial . 看一下Apache HttpClient并查看它的教程 HttpClient supports keep alive headers and other features that should enable you to programmatically make an HTTP call. HttpClient支持保持活动标头和其他功能,使您能够以编程方式进行HTTP调用。

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

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