引入Jar包
<!--spring restTemplate-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
HttpRequestUtils
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
public class MyHttpRequest {
private static RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
private static String token = "";
/**
* 获取完整的url
*/
private static String getUrl (String url) {
return "http://192.168.**.**:****/api/v1" + url;
}
/**
* 登录 获取token 需要加上定时任务
*/
public static void login() {
ResponseEntity<String> result = sendPost("/servicesmngt/login", "{\"user\":\"***\",\"password\":\"******\"}");
JSONObject reJsonObject = JSONObject.parseObject(result.getBody());
token = reJsonObject.getString("token");
System.out.println(new Date() + " 登录");
}
/**
* 上传文件
*/
public static ResponseEntity<String> uploadFiles(String url, MultipartFile file) {
// 配置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set("token", token);
// 处理参数
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("file",file.getResource());
//这里还可以加上别的参数 类似这句 parts.add("file",file.getResource());
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(parts, headers);
// 发起请求
ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.POST, httpEntity, String.class);
return res;
}
/**
* Get
*/
public static ResponseEntity<String> sendGet(String url, String param) {
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","application/json");
headers.set("token", token);
HttpEntity httpEntity = new HttpEntity(param, headers);
ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.GET, httpEntity, String.class);
return res;
}
/**
* Post
*/
public static ResponseEntity<String> sendPost(String url, String param) {
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","application/json");
headers.set("token", token);
HttpEntity httpEntity = new HttpEntity(param, headers);
ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.POST, httpEntity, String.class);
return res;
}
/**
* Patch
*/
public static ResponseEntity<String> sendPatch(String url, String param) {
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","application/json");
headers.set("token", token);
HttpEntity httpEntity = new HttpEntity(param, headers);
ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.PATCH, httpEntity, String.class);
return res;
}
/**
* Delete
*/
public static ResponseEntity<String> sendDelete(String url, String param) {
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","application/json");
headers.set("token", token);
HttpEntity httpEntity = new HttpEntity(param, headers);
ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.DELETE, httpEntity, String.class);
return res;
}
/**
* Put
*/
public static ResponseEntity<String> sendPut(String url, String param) {
HttpHeaders headers = new HttpHeaders();
headers.set("token", token);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity httpEntity = new HttpEntity(param, headers);
ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.PUT, httpEntity, String.class);
return res;
}
// end
}
另外:
这里的param都是json格式