其他分享
首页 > 其他分享> > http请求util

http请求util

作者:互联网

http请求util

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
public class HttpRequestUtil {
private static RestTemplate restTemplate = new RestTemplate();
public static String getRequest(String url) {
ResponseEntity responseEntity=restTemplate.getForEntity(url, String.class);
return (String) responseEntity.getBody();
}
public static Object getRequest(String url,Class clazz){
ResponseEntity responseEntity=restTemplate.getForEntity(url, clazz);
return responseEntity.getBody();
}
public static String postRequest(String url, Object parameters){
ResponseEntity responseEntity = restTemplate.postForEntity(url,parameters,String.class);
String body = (String) responseEntity.getBody();
return body;
}
public static String postRequestJson(String url, Object parameters){
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
HttpEntity<String> formEntity = new HttpEntity<String>(JsonUtil.toJson(parameters), headers);
ResponseEntity responseEntity = restTemplate.postForEntity(url,formEntity,String.class);
String body = (String) responseEntity.getBody();
return body;
}
public static Object postRequest(String url, Object parameters,Class clazz){
ResponseEntity responseEntity = restTemplate.postForEntity(url,parameters,clazz);
return responseEntity.getBody();
}
public static String postRequestFile(String url, Map<String,Object> formPost, FileSystemResource file, String filePostName){
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);

MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
for (String key : formPost.keySet()) {
form.add(key,formPost.get(key));
}
form.add(filePostName,file);
HttpEntity<MultiValueMap<String, Object>> post = new HttpEntity<>(form, headers);
return restTemplate.postForObject(url, post, String.class);
}
public static Object postRequestFile(String url, Map<String,Object> formPost, FileSystemResource file, String filePostName, Class clazz){
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);

MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
for (String key : formPost.keySet()) {
form.add(key,formPost.get(key));
}
HttpEntity<MultiValueMap<String, Object>> post = new HttpEntity<>(form, headers);
return restTemplate.postForObject(url, post, clazz);
}

public static String postRequestFile(String url, Map<String,Object> formPost, InputStreamSource iss, String filePostName){

HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);

MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
for (String key : formPost.keySet()) {
form.add(key,formPost.get(key));
}
form.add(filePostName,iss);
HttpEntity<MultiValueMap<String, Object>> post = new HttpEntity<>(form, headers);
return restTemplate.postForObject(url, post, String.class);
}

}

标签:http,请求,form,url,util,headers,responseEntity,new,String
来源: https://www.cnblogs.com/gnpugongying/p/15127876.html