HTTP请求封装类

import org.springframework.http.*;

import org.springframework.http.client.SimpleClientHttpRequestFactory;

import org.springframework.util.MultiValueMap;

import org.springframework.web.client.HttpClientErrorException;

import org.springframework.web.client.RestTemplate;

import java.io.IOException;

import java.util.Map;

public class HttpRestUtils {

/**

* http post

* */

public static String post(String url, MultiValueMap<String, String> params) throws IOException {

return httpRestClient(url, HttpMethod.POST, params);

}

/**

* http get

* */

public static String get(String url,Map<String, String> params) throws IOException {

return httpRestClientGET(url, HttpMethod.GET, params);

}

public static String httpRestClient(String url, HttpMethod method, MultiValueMap<String, String> params) throws IOException {

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();

requestFactory.setConnectTimeout(10*1000);

requestFactory.setReadTimeout(10*1000);

RestTemplate client = new RestTemplate(requestFactory);

HttpHeaders headers = new HttpHeaders();

HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);

// 执行HTTP请求

ResponseEntity<String> response = null;

try{

response = client.exchange(url, method, requestEntity, String.class,params);

return response.getBody();

}

catch (Exception e) {

System.out.println( "------------- HttpRestUtils.httpRestClient() 出现异常 Exception -------------");

System.out.println(e.getMessage());

return "";

}

}

public static String httpRestClientGET(String url, HttpMethod method, Map<String, String> params) throws IOException {

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();

requestFactory.setConnectTimeout(10*1000);

requestFactory.setReadTimeout(10*1000);

RestTemplate client = new RestTemplate(requestFactory);

HttpHeaders headers = new HttpHeaders();

// 执行HTTP请求

ResponseEntity<String> response = null;

try{

response = client.exchange(url, method, null, String.class,params);

return response.getBody();

}

catch (Exception e) {

System.out.println( "------------- HttpRestUtils.httpRestClient() 出现异常 Exception -------------");

System.out.println(e.getMessage());

return "";

}

}

public static String httpRestClientPOST(String url,Object object) throws Exception {

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();

requestFactory.setConnectTimeout(10*1000);

requestFactory.setReadTimeout(10*1000);

RestTemplate client = new RestTemplate(requestFactory);

ResponseEntity<String> response = null;

try{

response = client.postForEntity(url,object,String.class);

return response.getBody();

}

catch (Exception e) {

e.printStackTrace();

}

return "";

}

public static String httpRestClientGET(String url) throws Exception {

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();

requestFactory.setConnectTimeout(10*1000);

requestFactory.setReadTimeout(10*1000);

RestTemplate client = new RestTemplate(requestFactory);

ResponseEntity<String> response = null;

try{

response = client.getForEntity(url,String.class);

//client.postForEntity();

return response.getBody();

}

catch (Exception e) {

e.printStackTrace();

}

return "";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值