博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 发送POST,DELETE,PATCH,GET请求
阅读量:5775 次
发布时间:2019-06-18

本文共 4784 字,大约阅读时间需要 15 分钟。

import java.io.IOException;import org.apache.commons.codec.CharEncoding;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpDelete;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpOptions;import org.apache.http.client.methods.HttpPatch;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.methods.HttpPut;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.util.EntityUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * HTTP请求辅助工具 *  * @project iweixin-pay * @fileName WeixinUtil.java * @Description * @author light-zhang * @date 2018年5月29日下午3:29:42 * @version 1.0.0 */public class HttpUtils {    private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);    private static final HttpClient httpClient = HttpClientBuilder.create().build();    /**     * 发送POST请求     *      * @param url     * @param _class     * @return     */    public static 
T post(String url, Class
typeOfT) { try { HttpResponse response = httpClient.execute(new HttpPost(url)); HttpEntity entity = response.getEntity(); if (entity != null) { logger.debug("post httpRequest url info:{},response info:{}", url, response); return JsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 发送DELETE请求 * * @param url * @param typeOfT * @return */ public static
T delete(String url, Class
typeOfT) { try { HttpResponse response = httpClient.execute(new HttpDelete(url)); HttpEntity entity = response.getEntity(); if (entity != null) { logger.debug("delete httpRequest url info:{},response info:{}", url, response); return JsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 发送PATCH请求 * * @param url * @param typeOfT * @return */ public static
T patch(String url, Class
typeOfT) { try { HttpResponse response = httpClient.execute(new HttpPatch(url)); HttpEntity entity = response.getEntity(); if (entity != null) { logger.debug("patch httpRequest url info:{},response info:{}", url, response); return JsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 发送GET请求 * * @param url * @param obj * @return */ public static
T get(String url, Class
typeOfT) { try { HttpResponse response = httpClient.execute(new HttpGet(url)); HttpEntity entity = response.getEntity(); if (entity != null) { logger.debug("get httpRequest url info:{},response info:{}", url, response); return JsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 发送PUT请求 * * @param url * @param _class * @return */ public static
T put(String url, Class
typeOfT) { try { HttpResponse response = httpClient.execute(new HttpPut(url)); HttpEntity entity = response.getEntity(); if (entity != null) { logger.debug("put httpRequest url info:{},response info:{}", url, response); return JsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 发送OPTIONS请求 * * @param url * @param typeOfT * @return */ public static
T options(String url, Class
typeOfT) { try { HttpResponse response = httpClient.execute(new HttpOptions(url)); HttpEntity entity = response.getEntity(); if (entity != null) { logger.debug("options httpRequest url info:{},response info:{}", url, response); return JsonPoolUtils.fromJson(EntityUtils.toString(entity, CharEncoding.UTF_8), typeOfT); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }}

 

转载于:https://www.cnblogs.com/light-zhang/p/9869230.html

你可能感兴趣的文章
Resume简历中装B的词汇总结大全
查看>>
python udp编程实例
查看>>
TortoiseSVN中图标的含义
查看>>
js原生继承之——构造函数式继承实例
查看>>
linux定时任务的设置
查看>>
[CareerCup] 13.3 Virtual Functions 虚函数
查看>>
[Angular 2] ng-model and ng-for with Select and Option elements
查看>>
Visio中如何让重叠图形都显示
查看>>
Tasks and Back stack 详解
查看>>
关于EXPORT_SYMBOL的作用浅析
查看>>
成功的背后!(给所有IT人)
查看>>
在SpringMVC利用MockMvc进行单元测试
查看>>
Nagios监控生产环境redis群集服务战
查看>>
Angular - -ngKeydown/ngKeypress/ngKeyup 键盘事件和鼠标事件
查看>>
Android BlueDroid(一):BlueDroid概述
查看>>
Java利用httpasyncclient进行异步HTTP请求
查看>>
循环多少次? 【杭电--HDOJ-1799】 附题+具体解释
查看>>
宿舍局域网的应用
查看>>
html代码究竟什么用途
查看>>
oracle的substr函数的用法
查看>>