无法在java中使用cachingHttpClient缓存HttpResponse?

前端之家收集整理的这篇文章主要介绍了无法在java中使用cachingHttpClient缓存HttpResponse?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试使用cachingHttpClient缓存HTTP响应,但是徒劳无功.
这是我通过参考此链接http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html汇总的演示

  public class CacheDemo {

    public static void main(String[] args) {
        CacheConfig cacheConfig = new CacheConfig();
        cacheConfig.setMaxCacheEntries(1000);
        cacheConfig.setMaxObjectSizeBytes(1024 * 1024);

        HttpClient cachingClient = new CachingHttpClient(new DefaultHttpClient(),cacheConfig);

        HttpContext localContext = new BasicHttpContext();

        sendRequest(cachingClient,localContext);
        CacheResponseStatus responseStatus = (CacheResponseStatus) localContext.getAttribute(
                CachingHttpClient.CACHE_RESPONSE_STATUS);
        checkResponse(responseStatus);


        sendRequest(cachingClient,localContext);
        responseStatus = (CacheResponseStatus) localContext.getAttribute(
                CachingHttpClient.CACHE_RESPONSE_STATUS);
        checkResponse(responseStatus);
    }

    static void sendRequest(HttpClient cachingClient,HttpContext localContext) {
        HttpGet httpget = new HttpGet("http://www.mydomain.com/content/");
        HttpResponse response = null;
        try {
            response = cachingClient.execute(httpget,localContext);
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        HttpEntity entity = response.getEntity();
        try {
            EntityUtils.consume(entity);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    static void checkResponse(CacheResponseStatus responseStatus) {
        switch (responseStatus) {
            case CACHE_HIT:
                System.out.println("A response was generated from the cache with no requests "
                        + "sent upstream");
                break;
            case CACHE_MODULE_RESPONSE:
                System.out.println("The response was generated directly by the caching module");
                break;
            case CACHE_MISS:
                System.out.println("The response came from an upstream server");
                break;
            case VALIDATED:
                System.out.println("The response was generated from the cache after validating "
                        + "the entry with the origin server");
                break;
        }
    }

  }

它是一个简单的程序,但我无法弄清楚我哪里出错了.非常感谢您的帮助.谢谢.

最佳答案
url http://www.mydomain.com/content/的GET请求将以Http 404代码(未找到)结束.这个结果很可能不会被高速缓存,所以这就是为什么它对我不起作用的原因.

更新:
必须满足某些条件才能从缓存中提供响应.
您应该启用apache http client的日志记录(例如http://hc.apache.org/httpclient-3.x/logging.html).您可以调试正在进行的操作以及为什么您的其他URL存在缓存未命中.您可能应该下载该库的源代码并查看(http://hc.apache.org/downloads.cgi).特别是你会对org.apache.http.impl.client.cache.CachedResponseSuitabilityChecker类感兴趣.这也可以帮助您进行以下的库开发.

顺便说一句. http://muvireviews.com/celebrity/full_view/41/Shahrukh-khan返回此标题

Cache-Control:no-store,no-cache,must-revalidate,post-check = 0,pre-check = 0,max-age = 0,no-store

并且由于CachedResponseSuitabilityChecker中的if语句:

            if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equals(elt.getName())) {
                log.trace("Response contained NO CACHE directive,cache was not suitable");
                return false;
            }

缓存不会被使用.

祝好运 原文链接:https://www.f2er.com/java/438076.html

猜你在找的Java相关文章