zoukankan      html  css  js  c++  java
  • HttpUtils 发送HTTP/HTTPS/{get/post}请求

    import java.io.IOException;
    
    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.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.google.gson.Gson;
    
    /**
     * HTTP请求辅助工具
     * 
     * @project iweixin
     * @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);
    
        /**
         * 发送GET请求
         * 
         * @param url
         * @param obj
         * @return
         */
        public static <T> T doGet(String url, Class<T> _class) {
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse response = httpClient.execute(httpGet);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    logger.debug("*********************doGet-URL****************" + url);
                    return new Gson().fromJson(EntityUtils.toString(entity, "utf-8"), _class);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 发送POST请求
         * 
         * @param url
         * @param _class
         * @return
         */
        public static <T> T doPost(String url, Class<T> _class) {
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(url);
            try {
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    logger.debug(
                            "*********************doPost-URL****************" + url);
                    return new Gson().fromJson(EntityUtils.toString(entity, "utf-8"), _class);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
  • 相关阅读:
    Android笔记(三) 使得Activity之间可以跳转---Intent
    Python jQuery
    Python JavaScript BOM和DOM以及window对象
    Python JavaScript
    Python 前端CSS样式
    Python 前端CSS
    Python 前端 HTTP HTML标签
    Python mysql中的:视图 触发器 事务 存储过程 mysql内置函数 流程控制 b+树 索引 慢日志查询 权限管理
    Python pymysql模块
    Django进阶Model篇005
  • 原文地址:https://www.cnblogs.com/light-zhang/p/9117769.html
Copyright © 2011-2022 走看看