ResponseCache.getDefault() in Java

ResponseCache.getDefault(): This method is available in the java.net.ResponseCache class of Java.

Syntax:

ResponseCache java.net.ResponseCache.getDefault()

This method gets the system-wide response cache.

Parameters: NA

Returns: the system-wide ResponseCache.

Throws:

1. SecurityException - If a security manager has been installed and it denies NetPermission("getResponseCache")

Approach

Java

package com.ResponseCache;

import java.io.IOException;
import java.net.CacheRequest;
import java.net.CacheResponse;
import java.net.ResponseCache;
import java.net.URI;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class ResponseCachegetDefault {
    public static void main(String[] args) {
        ResponseCache responseCache = new ResponseCache() {
            @Override
            public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders)
                    throws IOException {
                return null;
            }

            @Override
            public CacheRequest put(URI uri, URLConnection conn) throws IOException {
                return null;
            }
        };
        ResponseCache.setDefault(responseCache);
        System.out.println(ResponseCache.getDefault());
    }
}

Output:

com.ResponseCache.ResponseCachegetDefault$1@4361bd48


Some other methods of ResponseCache class

get(URI, String, Map)This method retrieves the cached response based on the requested URI, the request method, and the request headers. Typically this method is called by the protocol handler before it sends out the request to get the network resource. If a cached response is returned, that resource is used instead.

ResponseCache.getDefault()This method gets the system-wide response cache.

put(URI, URLConnection)The protocol handler calls this method after a resource has been retrieved, and the ResponseCache must decide whether or not to store the resource in its cache.

ResponseCache.setDefault(ResponseCache)This method sets (or unsets) the system-wide cache.

No comments:

Post a Comment