java 在没有浏览器的情况下以编程方式保持 HTTP 会话活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7642659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:49:11  来源:igfitidea点击:

Programmatically keep HTTP Session Alive without browser

javahttpsessionjakarta-ee

提问by Fazal

For one of our requirements I am talking between two servers using HTTP protocol. The interaction is long running, where a user might not interact with the other site for pretty long intervals.

对于我们的要求之一,我在使用 HTTP 协议的两台服务器之间进行交谈。交互是长时间运行的,用户可能不会在很长一段时间内与其他站点交互。

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). Can anyone suggest a programmatic way in Java to achieve keep-alive behavior

根据我有限的理解,浏览器通过在 header 或 cookie 中传递 keep-alive 来处理这个问题(我完全不明白)。任何人都可以在 Java 中提出一种编程方式来实现保持活动的行为

回答by EMM

1.

1.

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

Simply paste this piece if code in your deployment descriptor (DD).
If you want to keep your session alive for a particular duration of time replace -1 with any positive numeric value.
Time specified here is in minutes.

只需将这段代码粘贴到部署描述符 (DD) 中即可。
如果您想让会话在特定的持续时间内保持活动状态,请将 -1 替换为任何正数值。
此处指定的时间以分钟为单位。

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 :

2.

如果您想更改特定会话实例的会话超时值而不影响应用程序中任何其他会话的超时长度:

session.setMaxInactiveInterval(30*60);



**********************
Note :

** *** *** *** *** *** *** **
注:

1.In DD, the time specified is in minutes.
2.If you do it programatically, the time specified is in seconds.

1.在DD中,指定的时间以分钟为单位
2.如果您以编程方式执行此操作,则指定的时间以单位

Hope this helps :)

希望这可以帮助 :)

回答by JustTry

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

回答by momo

Take a look of Apache HttpClientand see its tutorial. HttpClient supports keep alive headers and other features that should enable you to programmatically make an HTTP call.

查看Apache HttpClient并查看其教程。HttpClient 支持保持活动标头和其他功能,这些功能应该使您能够以编程方式进行 HTTP 调用。