zoukankan      html  css  js  c++  java
  • Apache的HttpClient,封装工具类

    引入依赖

            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.13</version>
            </dependency>

    1、GET方法

        public static String doGet(String url, Map<String, String> params) {
            CloseableHttpClient client = HttpClients.createDefault();
            URIBuilder uriBuilder;
            CloseableHttpResponse httpResponse = null;
            try {
                uriBuilder = new URIBuilder(url);
                if(params != null) {
                    params.forEach((k, v) -> {
                        uriBuilder.addParameter(k, v);
                    });
                }
                URI uri = uriBuilder.build();
                HttpGet httpGet = new HttpGet(uri);
                httpResponse = client.execute(httpGet);
                return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(httpResponse != null) {
                        httpResponse.close();
                    }
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

    2、 POST方法

      public static String doPost(String url, Map<String, String> params) {
            CloseableHttpClient client = HttpClients.createDefault();
            CloseableHttpResponse httpResponse = null;
            try {
                HttpPost httpPost = new HttpPost(url);
                if(params != null) {
                    List<NameValuePair> nvpList = new ArrayList<>();
                    params.forEach((k, v) -> {
                        nvpList.add(new BasicNameValuePair(k, v));
                    });
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvpList, Charset.defaultCharset());
                    httpPost.setEntity(entity);
                }
                httpResponse = client.execute(httpPost);
                return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(httpResponse != null) {
                        httpResponse.close();
                    }
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

    3、application/json方式传参

        public static String doPostJson(String url, Map<String, String> params) {
            CloseableHttpClient client = HttpClients.createDefault();
            CloseableHttpResponse httpResponse = null;
            try {
                HttpPost httpPost = new HttpPost(url);
                if(params != null) {
                    StringEntity entity = new StringEntity(JSON.toJSONString(params), ContentType.APPLICATION_JSON);
                    httpPost.setEntity(entity);
                }
                httpResponse = client.execute(httpPost);
                return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(httpResponse != null) {
                        httpResponse.close();
                    }
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

    贴上完整工具类

    import com.alibaba.fastjson.JSON;
    import net.jcip.annotations.ThreadSafe;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    import java.io.IOException;
    import java.net.URI;
    import java.nio.charset.Charset;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @ThreadSafe
    public class HttpClient {
    
        public static String doGet(String url) {
           return doGet(url, null);
        }
    
        public static String doGet(String url, Map<String, String> params) {
            CloseableHttpClient client = HttpClients.createDefault();
            URIBuilder uriBuilder;
            CloseableHttpResponse httpResponse = null;
            try {
                uriBuilder = new URIBuilder(url);
                if(params != null) {
                    params.forEach((k, v) -> {
                        uriBuilder.addParameter(k, v);
                    });
                }
                URI uri = uriBuilder.build();
                HttpGet httpGet = new HttpGet(uri);
                httpResponse = client.execute(httpGet);
                return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(httpResponse != null) {
                        httpResponse.close();
                    }
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
        public static String doPost(String url) {
            return doPost(url, null);
        }
    
        public static String doPost(String url, Map<String, String> params) {
            CloseableHttpClient client = HttpClients.createDefault();
            CloseableHttpResponse httpResponse = null;
            try {
                HttpPost httpPost = new HttpPost(url);
                if(params != null) {
                    List<NameValuePair> nvpList = new ArrayList<>();
                    params.forEach((k, v) -> {
                        nvpList.add(new BasicNameValuePair(k, v));
                    });
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvpList, Charset.defaultCharset());
                    httpPost.setEntity(entity);
                }
                httpResponse = client.execute(httpPost);
                return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(httpResponse != null) {
                        httpResponse.close();
                    }
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
        public static String doPostJson(String url, Map<String, String> params) {
            CloseableHttpClient client = HttpClients.createDefault();
            CloseableHttpResponse httpResponse = null;
            try {
                HttpPost httpPost = new HttpPost(url);
                if(params != null) {
                    StringEntity entity = new StringEntity(JSON.toJSONString(params), ContentType.APPLICATION_JSON);
                    httpPost.setEntity(entity);
                }
                httpResponse = client.execute(httpPost);
                return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(httpResponse != null) {
                        httpResponse.close();
                    }
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    
    }
  • 相关阅读:
    jenkins或ansible启动应用不成功日志又不报错
    zookeeper学习(2)----zookeeper和kafka的关系
    缺包与maven
    zookeeper入门(1)---基本概念
    6. kafka序列化和反序列化
    5.Kafka消费者-从Kafka读取数据(转)
    git学习(5)---git stash
    通过getResourceAsStream方法获取项目下的指定资源
    CommandLineParse类(命令行解析类)
    4.kafka生产者---向Kafka中写入数据(转)
  • 原文地址:https://www.cnblogs.com/zhexuejun/p/14837110.html
Copyright © 2011-2022 走看看