CacheResponse 是一个抽象类,表示从 ResponseCache 检索资源的通道。此类的对象提供 InputStream,它返回 entity-body 和关联的响应标头。该类继承了 java.lang.Object 的方法,例如克隆,等于,最终化,getClass(),hashCode(),notify(),notifyAll(),toString(),wait().
public abstract class CacheResponse extends Object
方法:CacheResponse类提供了两种方法,如下:
- getBody()方法
- getHeaders()方法
方法一: getBody()方法返回一个InputStream,可以从中访问响应正文。
用法:
public abstract InputStream getBody() throws IOException
参数: NA
返回类型:此方法以 InputStream 形式返回响应正文。
异常:获取响应标头时引发 I/O 异常。
执行:
示例 1
Java
// Java Program to illustrate CacheResponse Class
// showcasing getBody() method
// Importing general class of exception
// produced by Interrupted I/O exception
import java.io.IOException;
// Importing superclass of all IO classes
import java.io.InputStream;
// Importing Cacheresponse class from java.net package
// to create an applet
import java.net.CacheResponse;
// Importing List and Map classes
// from java.util package
import java.util.List;
import java.util.Map;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Creating an object of CacheResponse class
CacheResponse cr = new CacheResponse() {
// getHeaders() method returns response headers
// as Map
public Map<String, List<String> > getHeaders()
throws IOException
{
return null;
}
// getBody() method returns response body as
// InputStream
public InputStream getBody() throws IOException
{
System.out.println(
"getbody() has been tested");
return null;
}
};
// Returning an InputStream from which response body
// can be accessed
cr.getBody();
}
}
输出
getbody() has been tested
现在说明标题中前面讨论的另一种方法
方法二:getHeaders()方法返回从响应标头字段名称到字段值列表的不可变映射。
用法:
public abstract Map<String,List<String>> getHeaders() throws IOException
参数: NA
返回类型:标头的响应作为 Map。
异常:获取响应标头时引发 I/O 异常。
执行:
示例 2
Java
// Java Program to illustrate CacheResponse Class
// showcasing getHeaders() method
// Importing general class of exception
// produced by Interrupted I/O exception
import java.io.IOException;
// Importing superclass of all IO classes
import java.io.InputStream;
// Importing Cacheresponse class from java.net package
// to create an applet
import java.net.CacheResponse;
// Importing List, Linkedlist, Map, Tree, TreeMap classes
// from java.util package
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
// Main class
// To illustrate getHeaders() method
public class GFG {
// main driver method
public static void main(String[] args) throws IOException {
// Creating an object of CacheResponse class
CacheResponse cr = new CacheResponse() {
// getHeaders() method returns response headers as Map
public Map<String, List<String>> getHeaders() throws IOException {
// Creating an object of Map class
// Object is of type- Integer and List<String>
Map<Integer, List<String>> map = new TreeMap<Integer, List<String>>();
// Creating an object of List class
List<String> list= new LinkedList<String>();
// Adding element to List object created above
// using add() method
list.add("GFG");
// Adding element to map object created above
// using put() method
map.put(1,list);
// Print Map class object element's
System.out.println(map);
return null;
}
// getBody() method returns response body as InputStream
public InputStream getBody() throws IOException {
return null;
}
};
// Returning an immutable Map from response header
cr.getHeaders();
}
}
输出
{1=[GFG]}
相关用法
- Java java.net.CacheRequest用法及代码示例
- Java java.net.CookiePolicy用法及代码示例
- Java java.net.CookieStore用法及代码示例
- Java java.net.CookieHandler用法及代码示例
- Java java.net.CookieManager用法及代码示例
- Java java.net.SocketException用法及代码示例
- Java java.net.Proxy用法及代码示例
- Java java.net.ProxySelector用法及代码示例
- Java java.net.ProtocolFamily用法及代码示例
- Java java.net.SocketOption用法及代码示例
- Java java.net.SecureCacheResponse用法及代码示例
- Java java.net.SocketImplFactory用法及代码示例
- Java java.net.ResponseCache用法及代码示例
- Java java.net.URLPermission用法及代码示例
- Java java.net.NetPermission用法及代码示例
- Java java.net.FileNameMap用法及代码示例
- Java java.net.PasswordAuthentication用法及代码示例
- Java java.net.BindException用法及代码示例
- Java java.net.URLConnection用法及代码示例
- Java java.net.Socket用法及代码示例
- Java java.net.ServerSocket用法及代码示例
- Java java.net.InetAddress用法及代码示例
- Java java.nio.ByteBuffer用法及代码示例
- Java java.nio.IntBuffer用法及代码示例
- Java java.nio.file.FileStore用法及代码示例
注:本文由纯净天空筛选整理自surbhityagi15大神的英文原创作品 java.net.CacheResponse Class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。