http发送工具类 springboot里调用别人的api
`@Slf4j
@Component
public class HttpClientSender {
@Autowired
@Qualifier("normalRestTemplate")
private RestTemplate restTemplate;
public String sendHttpPost(String host, String method, String sendData) throws IotException {
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<>(sendData, headers);
String url = host + "/" + method;
log.debug("HttpClientSenderurl.sendHttpPost url:{}, request: {}", url, sendData);
String response;
try {
response = restTemplate.postForObject(url, formEntity, String.class);
} catch (Exception e) {
log.error("HttpClientSender.sendHttpPost failed call {}, {}", url, e);
throw new IotException(ResponseCode.INTERFACE_OUTTER_INVOKE_ERROR);
}
log.debug("HttpClientSender.sendHttpPost succeed call {}, result: {}", url, response);
return response;
}
public String sendHttpGet(String host, String method, String urlParams) throws IotException {
String url = host + "/" + method + urlParams;
log.debug("HttpClientSender.sendHttpGet url:{}", url);
String response;
try {
response = restTemplate.getForObject(url, String.class);
} catch (Exception e) {
log.error("HttpClientSender.sendHttpGet failed call {}, {}", url, e);
throw new IotException(ResponseCode.INTERFACE_OUTTER_INVOKE_ERROR);
}
log.debug("HttpClientSender.sendHttpGet succeed call {}, result: {}", url, response);
return response;
}
}`
浙公网安备 33010602011771号