zoukankan      html  css  js  c++  java
  • java 发送POST,DELETE,PATCH,GET请求

    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> T post(String url, Class<T> 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> T delete(String url, Class<T> 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> T patch(String url, Class<T> 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> T get(String url, Class<T> 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> T put(String url, Class<T> 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> T options(String url, Class<T> 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;
        }
    }
  • 相关阅读:
    Intellij使用心得(四) -- 导入Eclipse的代码格式化文件
    idea安装插件plugin(主要针对网络连接不上的情况)
    IDEA破解方法以及快捷键大全
    eclipse+maven搭建自己web系统的骨架,解决自带骨架加载无限慢的问题
    用Eclipse创建Maven多模块项目
    Maven实战--- dependencies与dependencyManagement的区别
    redis之有序集合类型(Zset)——排行榜的实现
    做一个完整的Java Web项目需要掌握的技能
    经典面试题:用户反映你开发的网站访问很慢可能会是什么原因
    linux 免密码 使用sudo 直接使用root权限执行命令
  • 原文地址:https://www.cnblogs.com/light-zhang/p/9869230.html
Copyright © 2011-2022 走看看