36 lines
1.2 KiB
Plaintext
36 lines
1.2 KiB
Plaintext
|
|
package com.sercurityControl.proteam.util;
|
|||
|
|
|
|||
|
|
import org.springframework.http.*;
|
|||
|
|
import org.springframework.web.client.RestTemplate;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @author bonus
|
|||
|
|
* @data 2023/1/5 15:14
|
|||
|
|
* @description 工具类
|
|||
|
|
*/
|
|||
|
|
public class HttpUtils {
|
|||
|
|
static RestTemplate client = new RestTemplate();
|
|||
|
|
/**
|
|||
|
|
* GET/POST 请求
|
|||
|
|
*
|
|||
|
|
* @param url 地址
|
|||
|
|
* @param params 参数
|
|||
|
|
* @param type 请求类型 false:GET,true:POST
|
|||
|
|
* @return 数据
|
|||
|
|
*/
|
|||
|
|
public static String sendRequest(String url, Object params, boolean type) {
|
|||
|
|
//新建Http头,add方法可以添加参数
|
|||
|
|
HttpHeaders headers = new HttpHeaders();
|
|||
|
|
//设置请求发送方式
|
|||
|
|
HttpMethod method = type ? HttpMethod.POST : HttpMethod.GET;
|
|||
|
|
//以表单的方式提交
|
|||
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|||
|
|
//将请求头部和参数合成一个请求
|
|||
|
|
HttpEntity<Object> requestEntity = new HttpEntity<>(params, headers);
|
|||
|
|
//执行HTTP请求,将返回的结构使用String类格式化(可以设置为对应返回值格式的类)
|
|||
|
|
ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
|
|||
|
|
|
|||
|
|
return response.getBody();
|
|||
|
|
}
|
|||
|
|
}
|