当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。