本文整理汇总了Java中java.net.CacheResponse.getBody方法的典型用法代码示例。如果您正苦于以下问题:Java CacheResponse.getBody方法的具体用法?Java CacheResponse.getBody怎么用?Java CacheResponse.getBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.CacheResponse
的用法示例。
在下文中一共展示了CacheResponse.getBody方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: get
import java.net.CacheResponse; //导入方法依赖的package包/类
@Override public CacheResponse get(URI uri, String requestMethod,
Map<String, List<String>> requestHeaders) throws IOException {
final CacheResponse response = delegate.get(uri, requestMethod, requestHeaders);
if (response instanceof SecureCacheResponse) {
return new CacheResponse() {
@Override public InputStream getBody() throws IOException {
return response.getBody();
}
@Override public Map<String, List<String>> getHeaders() throws IOException {
return response.getHeaders();
}
};
}
return response;
}
示例2: fetchFromHTTPUrlConnectionCache
import java.net.CacheResponse; //导入方法依赖的package包/类
/**
* Fetches an entry value from the HttpResponseCache cache
* @param connection connection from which we need the cache
* @param uri uri to use to get the cache entry
* @return cache entry value as String
*/
private String fetchFromHTTPUrlConnectionCache(HttpURLConnection connection, URI uri) {
try {
HttpResponseCache responseCache = HttpResponseCache.getInstalled();
if(responseCache != null){
CacheResponse cacheResponse = responseCache.get(uri, "GET", connection.getRequestProperties());
Scanner scanner = new Scanner(cacheResponse.getBody(), "UTF-8");
StringBuilder sb = new StringBuilder();
while (scanner.hasNextLine()){
sb.append(scanner.nextLine());
}
return sb.toString();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
示例3: initResponseSource
import java.net.CacheResponse; //导入方法依赖的package包/类
/**
* Initialize the source for this response. It may be corrected later if the
* request headers forbids network use.
*/
private void initResponseSource() throws IOException {
responseSource = ResponseSource.NETWORK;
if (!policy.getUseCaches()) return;
OkResponseCache responseCache = client.getOkResponseCache();
if (responseCache == null) return;
CacheResponse candidate = responseCache.get(
uri, method, requestHeaders.getHeaders().toMultimap(false));
if (candidate == null) return;
Map<String, List<String>> responseHeadersMap = candidate.getHeaders();
cachedResponseBody = candidate.getBody();
if (!acceptCacheResponseType(candidate)
|| responseHeadersMap == null
|| cachedResponseBody == null) {
Util.closeQuietly(cachedResponseBody);
return;
}
RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(responseHeadersMap, true);
cachedResponseHeaders = new ResponseHeaders(uri, rawResponseHeaders);
long now = System.currentTimeMillis();
this.responseSource = cachedResponseHeaders.chooseResponseSource(now, requestHeaders);
if (responseSource == ResponseSource.CACHE) {
this.cacheResponse = candidate;
setResponse(cachedResponseHeaders, cachedResponseBody);
} else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
this.cacheResponse = candidate;
} else if (responseSource == ResponseSource.NETWORK) {
Util.closeQuietly(cachedResponseBody);
} else {
throw new AssertionError();
}
}
示例4: initResponseSource
import java.net.CacheResponse; //导入方法依赖的package包/类
/**
* Initialize the source for this response. It may be corrected later if the
* request headers forbids network use.
*/
private void initResponseSource() throws IOException {
responseSource = ResponseSource.NETWORK;
if (!policy.getUseCaches())
return;
OkResponseCache responseCache = client.getOkResponseCache();
if (responseCache == null)
return;
CacheResponse candidate = responseCache.get(uri, method, requestHeaders.getHeaders().toMultimap(false));
if (candidate == null)
return;
Map<String, List<String>> responseHeadersMap = candidate.getHeaders();
cachedResponseBody = candidate.getBody();
if (!acceptCacheResponseType(candidate) || responseHeadersMap == null || cachedResponseBody == null) {
Util.closeQuietly(cachedResponseBody);
return;
}
RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(responseHeadersMap, true);
cachedResponseHeaders = new ResponseHeaders(uri, rawResponseHeaders);
long now = System.currentTimeMillis();
this.responseSource = cachedResponseHeaders.chooseResponseSource(now, requestHeaders);
if (responseSource == ResponseSource.CACHE) {
this.cacheResponse = candidate;
setResponse(cachedResponseHeaders, cachedResponseBody);
} else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
this.cacheResponse = candidate;
} else if (responseSource == ResponseSource.NETWORK) {
Util.closeQuietly(cachedResponseBody);
} else {
throw new AssertionError();
}
}