java patch_java 发送POST,DELETE,PATCH,GET请求

importjava.io.IOException;importorg.apache.commons.codec.CharEncoding;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpDelete;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpOptions;importorg.apache.http.client.methods.HttpPatch;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.methods.HttpPut;importorg.apache.http.impl.client.HttpClientBuilder;importorg.apache.http.util.EntityUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;/*** HTTP请求辅助工具

*

* @project iweixin-pay

* @fileName WeixinUtil.java

* @Description

*@authorlight-zhang

* @date 2018年5月29日下午3:29:42

*@version1.0.0*/

public classHttpUtils {private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);private static final HttpClient httpClient =HttpClientBuilder.create().build();/*** 发送POST请求

*

*@paramurl

*@param_class

*@return

*/

public static T post(String url, ClasstypeOfT) {try{

HttpResponse response= httpClient.execute(newHttpPost(url));

HttpEntity entity=response.getEntity();if (entity != null) {

logger.debug("post httpRequest url info:{},response info:{}", url, response);returnJsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT);

}

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}return null;

}/*** 发送DELETE请求

*

*@paramurl

*@paramtypeOfT

*@return

*/

public static T delete(String url, ClasstypeOfT) {try{

HttpResponse response= httpClient.execute(newHttpDelete(url));

HttpEntity entity=response.getEntity();if (entity != null) {

logger.debug("delete httpRequest url info:{},response info:{}", url, response);returnJsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT);

}

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}return null;

}/*** 发送PATCH请求

*

*@paramurl

*@paramtypeOfT

*@return

*/

public static T patch(String url, ClasstypeOfT) {try{

HttpResponse response= httpClient.execute(newHttpPatch(url));

HttpEntity entity=response.getEntity();if (entity != null) {

logger.debug("patch httpRequest url info:{},response info:{}", url, response);returnJsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT);

}

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}return null;

}/*** 发送GET请求

*

*@paramurl

*@paramobj

*@return

*/

public static T get(String url, ClasstypeOfT) {try{

HttpResponse response= httpClient.execute(newHttpGet(url));

HttpEntity entity=response.getEntity();if (entity != null) {

logger.debug("get httpRequest url info:{},response info:{}", url, response);returnJsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT);

}

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}return null;

}/*** 发送PUT请求

*

*@paramurl

*@param_class

*@return

*/

public static T put(String url, ClasstypeOfT) {try{

HttpResponse response= httpClient.execute(newHttpPut(url));

HttpEntity entity=response.getEntity();if (entity != null) {

logger.debug("put httpRequest url info:{},response info:{}", url, response);returnJsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT);

}

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}return null;

}/*** 发送OPTIONS请求

*

*@paramurl

*@paramtypeOfT

*@return

*/

public static T options(String url, ClasstypeOfT) {try{

HttpResponse response= httpClient.execute(newHttpOptions(url));

HttpEntity entity=response.getEntity();if (entity != null) {

logger.debug("options httpRequest url info:{},response info:{}", url, response);returnJsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT);

}

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}return null;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值