【HttpClient:Get和Post请求】

1、添加依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

2、HttpClientUtil工具类

package com.task.test.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

@Slf4j
public class HttpClientUtil {

   public static String doGet(String url) {
      CloseableHttpClient closeableHttpClient = null;
      HttpGet httpGet = null;
      String respone = null;
      try{
         closeableHttpClient = HttpClientBuilder.create().build();
         RequestConfig build = RequestConfig.custom().setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(10000).build();
         httpGet = new HttpGet(url);
         httpGet.setConfig(build);
         CloseableHttpResponse execute = closeableHttpClient.execute(httpGet);
         if(execute!=null){
            HttpEntity entity = execute.getEntity();
            if(entity!=null){
               respone = EntityUtils.toString(entity,"UTF-8");
            }
         }
         execute.close();
      }catch (Exception e){
         log.info("请求接口url:{},异常信息:{}",url,e.getMessage());
          e.printStackTrace();
      }finally {
         try{
            closeableHttpClient.close();
         }catch (Exception e){
            e.getMessage();
         }
      }
      return respone;
   }



   public static String doPost(String url,String context){
      CloseableHttpClient  httpClient = null;
      HttpPost httpPost = null;
      String result = null;
      try{
         httpClient = HttpClientBuilder.create().build();
         RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(10000).setSocketTimeout(10000).setConnectTimeout(10000).build();
         httpPost = new HttpPost(url);
         httpPost.setConfig(requestConfig);
         StringEntity stringEntity = new StringEntity(context,"utf-8");
         stringEntity.setContentEncoding("UTF-8");
         stringEntity.setContentType("application/json");
         httpPost.setEntity(stringEntity);
         CloseableHttpResponse  response = httpClient.execute(httpPost);
         if(response != null){
            HttpEntity resEntity = response.getEntity();
            if(resEntity != null){
               result = EntityUtils.toString(resEntity,"UTF-8");
            }
         }
         response.close();
         System.out.print(httpClient);
      }catch(Exception ex){
         log.info("请求接口url:{},异常信息:{}",url,ex.getMessage());
         ex.printStackTrace();
      } finally{
         try {
            httpClient.close();
         } catch (IOException  e) {
            e.printStackTrace();
         }
      }
      return result;
   }

}

3、Get请求

public static final String ACCESS_TOKEN_URL = "http://localhost:9090/crud/list?name=NAME&classId=CLASSID";
@GetMapping("/list")
public String list() throws IOException {
   String replace = ACCESS_TOKEN_URL.replace("NAME", "王五").replace("CLASSID", "3");
   String result = HttpClient.doGet(replace);
   return result;
}

 4、Post请求

@PostMapping("/add")
public String add(@RequestBody TbTemplatePublic tbTemplatePublic){
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("templateType", tbTemplatePublic.getTemplateType());
    jsonObject.put("policeCode", tbTemplatePublic.getPoliceCode());
    jsonObject.put("templateContent", tbTemplatePublic.getTemplateContent());
    jsonObject.put("templateTitle", tbTemplatePublic.getTemplateTitle());
    jsonObject.put("fraudType", tbTemplatePublic.getFraudType());
    String result = HttpClientUtil.doPost("http://localhost:12006/templatePublic/add", jsonObject.toJSONString());
    return result;
}

如果写的有不对的地方,请大家指出来,我们一起进步,谢谢。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值