http调用端HttpClient、DefaultHttpClient、CloseableHttpClient

时间:2023-03-09 09:49:24
http调用端HttpClient、DefaultHttpClient、CloseableHttpClient

1:说下httpClient接口和4.2.6版本后过时实例DefaultHttpClient,以及新的实例应用。

   说到HTTP,脑子就冒出它的特性,基于TCP协议,简短点:说明是交互性的。

2:下面说平时开发中常用的Post请求:

      HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost("请求地址");
         List<NameValuePair> formparams = new ArrayList<NameValuePair>();
         formparams.add(new BasicNameValuePair("data", jsondata));
         System.out.println("==== 提交参数 ======" +formparams);
         UrlEncodedFormEntity uefEntity;
         try {
             uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
             httppost.setEntity(uefEntity);
             HttpResponse response = httpclient.execute(httppost);
             HttpEntity entity = response.getEntity();
             if(entity!=null){
                 String results=EntityUtils.toString(entity, "UTF-8");
                 System.out.println("接口返回值="+results);
             }
         } catch (ClientProtocolException e) {
             e.printStackTrace();
         } catch (UnsupportedEncodingException e1) {
             e1.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             // 关闭连接,释放资源
             httpclient.getConnectionManager().shutdown();
         }

HttpClient httpclient = new DefaultHttpClient();
现目前这么写的很多,如果在引用版本4.2.6或者以下的话,这个是没有过时提示的,也没有问题。
但引用的版本已经高于4.2.6了,还用这个方法就会提示过时,这个我专门去apache的httpclient看了下,从4.3版本后DefaultHttpClient这个实例方法就被标注过时了,
都说程序员看不到错误是不会去动它的,警告神马的就不管了,工期要紧。

3:替换新的实例CloseableHttpClient和CloseableHttpResponse:

 

 //声明新的httpclient
         CloseableHttpClient httpClient = HttpClients.createDefault();
         HttpPost httpPost = new HttpPost("请求地址");
         //拼接参数
         List<NameValuePair> list = new ArrayList<NameValuePair>();
         list.add(new BasicNameValuePair("data", jsonData));
         System.out.println("==== 提交参数 ======" +list);
         CloseableHttpResponse response  = null;
         try {
             httpPost.setEntity(new UrlEncodedFormEntity(list));
             response = httpClient.execute(httpPost);
             System.out.println("========请求状态========"+response.getStatusLine());
             HttpEntity entity = response.getEntity();
             if(entity != null){
                 String result = EntityUtils.toString(entity, "UTF-8");
                 System.out.println("========接口返回=======" +result);
             }
             EntityUtils.consume(entity);
         } catch (Exception e) {
             e.printStackTrace();
         }finally {
             if(response != null){
                 try {
                     response.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }

  可以看到几个新的类,CloseableHttpResponse响应调用后的参数接收,EntityUtils.consume(entity); 用之前的版本这个也是过时的,作用是关闭流功能。

  以前的方法httpclient.getConnectionManager().shutdown(); 顾名思义,调用端停止调用,和consume(关闭调用时返回流)这个功能是类似,但是到底有哪些不同,我还需要再研究研究。

文笔见拙,各位看官见谅。