建议使用响应处理程序处理HTTP响应.在本章中,我们将讨论如何创建响应处理程序以及如何使用它们来处理响应.
如果使用响应处理程序,将自动释放所有HTTP连接.
HttpClient API在包<中提供了一个名为 ResponseHandler 的接口b> org.apache.http.client.为了创建响应处理程序,实现此接口
并覆盖其 handleResponse()方法.
每个响应都有一个状态代码,如果状态代码在200到300之间,则表示该操作已成功接收,理解和接受.因此,在我们的示例中,我们将使用此类状态代码处理响应的实体.
按照步骤操作下面给出使用响应处理程序执行请求.
createDefault() HttpClients 类的方法返回类 CloseableHttpClient 的对象,它是 HttpClient 接口的基本实现.使用此方法创建一个HttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
使用以下代码行实现上面创建的响应处理程序对象&minus ;
ResponseHandler<String> responseHandler = new MyResponseHandler();
HttpGet 类表示HTTP GET请求使用URI检索给定服务器的信息.
通过实例化HttpGet类并将表示URI作为参数的字符串传递给其构造函数来创建HttpGet请求.
ResponseHandler<String> responseHandler = new MyResponseHandler();
CloseableHttpClient 类具有 execute()方法的变体,它接受两个对象 ResponseHandler 和HttpUriRequest,并返回一个响应对象.
String httpResponse = httpclient.execute(httpget, responseHandler);
以下示例演示了响应处理程序的用法.
import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; class MyResponseHandler implements ResponseHandler<String>{ public String handleResponse(final HttpResponse response) throws IOException{ //Get the status of the response int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); if(entity == null) { return ""; } else { return EntityUtils.toString(entity); } } else { return ""+status; } } } public class ResponseHandlerExample { public static void main(String args[]) throws Exception{ //Create an HttpClient object CloseableHttpClient httpclient = HttpClients.createDefault(); //instantiate the response handler ResponseHandler<String> responseHandler = new MyResponseHandler(); //Create an HttpGet object HttpGet httpget = new HttpGet("http://www.IT屋.com/"); //Execute the Get request by passing the response handler object and HttpGet object String httpresponse = httpclient.execute(httpget, responseHandler); System.out.println(httpresponse); } }
以上程序生成以下输出 :
<!DOCTYPE html> <!--[if IE 8]><html class = "ie ie8"> <![endif]--> <!--[if IE 9]><html class = "ie ie9"> <![endif]--> <!--[if gt IE 9]><!--> <html lang = "en-US"> <!--<![endif]--> <head> <!-- Basic --> <meta charset = "utf-8"> <meta http-equiv = "X-UA-Compatible" content = "IE = edge"> <meta name = "viewport" content = "width = device-width,initial-scale = 1.0,userscalable = yes"> <link href = "https://img01.yuandaxia.cn/Content/img/tutorials/apache_httpclient/mui-rem.min.css" rel = "stylesheet" type = "text/css" /> <link rel = "stylesheet" href = "/questions/css/home.css?v = 3" /> <script src = "/questions/js/jquery.min.js"></script> <script src = "/questions/js/fontawesome.js"></script> <script src = "https://cdn.muicss.com/mui-0.9.39/js/mui.min.js"></script> </head> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . <script> window.dataLayer = window.dataLayer || []; function gtag() {dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-232293-17'); </script> </body>