Anggrayudi H
Anggrayudi H

Reputation: 15165

Fetching expiry of the session from URL

I'm trying to fetch information from a URL, i.e. to know When the session time has expired from URL? For example, if you trying to connect to a URL and downloading a file with IDM from Mediafire, then you pause it. After 4 hours since your first connection to the Mediafire's URL, you're trying to re-connect and resume your download. Unfortunately, you get a message saying:

403: Forbidden. You don't have permission to access file.zip on this server.

or this message:

Probably this site uses temporary links that cannot be requested twice or expire after some time when session is completed.

It means that Mediafire gives us max time to re-connect. So you have to resume your download in that interval.

I have tried:

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
String exp = urlConnection.getHeaderField("Expires");
System.out.println("Expire date: " + exp);

Output:

Expire date: null // while responseCode is 200 OK

Sometimes, with different URL, I got Tue, 03 Nov 2015 03:41:09 PST.

So, how do I know when the session time has expired certainly?

Thank in advance.

Upvotes: 0

Views: 766

Answers (1)

Murat Karagöz
Murat Karagöz

Reputation: 37604

According to RFC there is not always a expires header field.

Section 5.3

An origin server without a clock MUST NOT generate an Expires field unless its value represents a fixed time in the past (always expired) or its value has been associated with the resource by a system or user with a reliable clock.

Furthermore it defines the Freshness in 4.2

If an origin server wishes to force a cache to validate every request, it can assign an explicit expiration time in the past to indicate that the response is already stale. Compliant caches will normally validate a stale cached response before reusing it for subsequent requests (see Section 4.2.4).

Since origin servers do not always provide explicit expiration times, caches are also allowed to use a heuristic to determine an expiration time under certain circumstances (see Section 4.2.2).

So in order to get a somewhat reliable headerfield you can calculate it

Since origin servers do not always provide explicit expiration times, a cache MAY assign a heuristic expiration time when an explicit time is not specified, employing algorithms that use other header field values (such as the Last-Modified time) to estimate a plausible expiration time. This specification does not provide specific algorithms, but does impose worst-case constraints on their results.

Upvotes: 1

Related Questions