Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically keep HTTP Session Alive without browser

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

like image 662
Fazal Avatar asked Oct 04 '11 02:10

Fazal


People also ask

How do I keep my HTTP session alive?

Keep-Alive is enabled by explicitly requesting it via HTTP header. If you don't have access to your web server configuration file, you can add HTTP headers yourself by using . htaccess file.

How do I keep a PHP session alive?

On the home page, to maintain the session, the session_start() function is called. This enables us to retrieve session variables from this page. Using time() function, the current time can be calculated.

How to maintain session?

Since HTTP and Web Server both are stateless, the only way to maintain a session is when some unique information about the session (session id) is passed between server and client in every request and response. There are several ways through which we can provide unique identifier in request and response.

How to keep session alive in ASP net c#?

Solution 1Place a timer on the ASP.NET page, set the interval time, i.e. 1 minute. and add UpdatePanel to handle the timer1 tick event asynchronously. at the code behind, inside the timer's tick event, you do nothing. once the timer1 raises the tick event, the session time out is reset and kept alive.


2 Answers

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

like image 198
EMM Avatar answered Sep 30 '22 20:09

EMM


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);
like image 27
JustTry Avatar answered Sep 30 '22 18:09

JustTry