因为要简化手工给 elasticsearch 传输文件的过程,所以需要使用 HttpClient 作为传输工具。第一次接触,从基础开始研究。
首先是 HttpClient 与 CloseableHttpClient 的差别。注意到这一点,是因为自己将网上的博客源码粘贴到IDEA 之后,发现创建 HttpClient 对象的语句HttpClient client = new DefaultHttpClient();
当中的 DefaultHttpClient 被标注为过时;此时又看到一个 doPost 方法中使用了 CloseableHttpClient httpclient = HttpClients.createDefault();
,于是很好奇二者间的差别。
网上关于二者间差异的文章不多,即便是翻墙也没有找到什么有用的内容。于是去 Apache 官网查找相关文档。CloseableHttpClient官方描述与HttpClient官方描述。
从文档中可以看到,HttpClient 是一个接口,而 CloseableHttpClient 则实现了该接口以及 java 的 Closeable 接口。
HttpClient:
共有10个方法,其中8个为重载,剩下的两个,一个用于构造 CloseableHttpClient 的实例,一个实现了 Cloneable 接口。
重载方法:
第一组
- HttpResponse( HttpUriRequest request ) throws IOException, ClientProtocolException
- HttpResponse( HttpUriRequest request, HttpContext context ) throws IOException, ClientProtocolException
第二组
- HttpResponse( HttpHost target, HttpRequest request ) throws IOException, ClientProtocolException
- HttpResponse( HttpHost target, HttpRequest request HttpContext context ) throws IOException, ClientProtocolException
泛型处理:
第一组
- HttpResponse( HttpUriRequest request, ResponseHandler<? extends T> responseHandler ) throws IOException, ClientProtocolException
- HttpResponse( HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context ) throws IOException, ClientProtocolException
第二组
- HttpResponse( HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler ) throws IOException, ClientProtocolException
- HttpResponse( HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context ) throws IOException, ClientProtocolException