import org.apache.http.client.CacheControl;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.cache.CacheConfig;
public class HttpClientExample {
public static void main(String[] args) throws Exception {
CacheConfig cacheConfig = CacheConfig.custom()
.setMaxCacheEntries(1000)
.setMaxObjectSize(1024 * 1024)
.build();
HttpClient httpClient = HttpClientBuilder.create()
.setDefaultCacheConfig(cacheConfig)
.setCacheDir(new File("/path/to/cache/directory"))
.build();
HttpGet httpGet = new HttpGet("http://example.com/api/data");
httpGet.addHeader("Cache-Control", "max-age=3600");
HttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response: " + responseBody);
}
}
}