以编程方式保持 HTTP 会话活动,无需浏览器

发布于 2024-12-07 14:59:14 字数 291 浏览 0 评论 0原文

对于我们的要求之一,我正在使用 HTTP 协议在两个服务器之间进行通信。交互是长时间运行的,用户可能不会在相当长的时间间隔内与其他站点交互。

当他们来到该页面时,登录到远程站点。每次用户尝试与远程站点交互时,我都会在内部进行 HTTP 调用(身份验证是基于 sessionId 完成的)。

我想知道是否有办法刷新会话并确保它不会过期。

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

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.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄断弥空 2024-12-14 14:59:14

1.

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

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

2.

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

session.setMaxInactiveInterval(30*60);

<强>**********************
注意:

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

希望这有帮助:)

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.

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.
2.If you do it programatically, the time specified is in seconds.

Hope this helps :)

灼痛 2024-12-14 14:59:14

我想下面的代码可以帮助你,如果你可以传递 JSESSIONID cookie,那么你的容器将负责保持会话处于活动状态,如果它的会话可能是从其他浏览器创建的。

找到下面的链接,解释了很多。

单击此处

上面链接中的代码片段

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

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.

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);
旧时浪漫 2024-12-14 14:59:14

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文