📜  Java的.net.CacheResponse类在Java中

📅  最后修改于: 2022-05-13 01:54:55.208000             🧑  作者: Mango

Java的.net.CacheResponse类在Java中

CacheResponse 是一个抽象类,表示从 ResponseCache 检索资源的通道。此类的对象提供一个 InputStream,它返回实体主体和关联的响应头。此类从Java.lang.Object 继承方法,如 clone、equals、finalize、 getClass()hashCode()notify()notifyAll()toString()wait()

public abstract class CacheResponse  extends Object

方法: CacheResponse 类提供了两种方法,如下所示:

  1. getBody()方法
  2. getHeaders()方法

方法 1: 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/Oxception
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 > 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();
    }
}


Java
// Java Program to illustrate CacheResponse Class
// showcasing getHeaders() method
 
// Importing general class of exception
// produced by Interrupted I/Oxception
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, Linkedist, 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> getHeaders() throws IOException { 
 
               // Creating an object of Map class
               // Object is of type- Integer and List
               Map> map = new TreeMap>(); 
                
               // Creating an object of List class
               List list= new LinkedList(); 
                
               // Adding element to List object creaed  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();
}
}


输出
getbody() has been tested

现在说明前面在标题中讨论的另一种方法

方法 2: getHeaders()方法返回一个从响应头字段名称到字段值列表的不可变映射。

句法:



public abstract Map> 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/Oxception
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, Linkedist, 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> getHeaders() throws IOException { 
 
               // Creating an object of Map class
               // Object is of type- Integer and List
               Map> map = new TreeMap>(); 
                
               // Creating an object of List class
               List list= new LinkedList(); 
                
               // Adding element to List object creaed  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]}