當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java java.net.CacheResponse用法及代碼示例


CacheResponse 是一個抽象類,表示從 ResponseCache 檢索資源的通道。此類的對象提供 InputStream,它返回 entity-body 和關聯的響應標頭。該類繼承了 java.lang.Object 的方法,例如克隆,等於,最終化,getClass(),hashCode(),notify(),notifyAll(),toString(),wait().

public abstract class CacheResponse  extends Object

方法:CacheResponse類提供了兩種方法,如下:

  1. getBody()方法
  2. 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]}


相關用法


注:本文由純淨天空篩選整理自surbhityagi15大神的英文原創作品 java.net.CacheResponse Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。