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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。