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;
        }
    
    }
  • 相关阅读:
    (七)策略模式详解
    (六)观察者模式详解(包含观察者模式JDK的漏洞以及事件驱动模型)
    递归锁,死锁,使用递归锁解决死锁,信号量
    并发编程中的GIL锁(全局解释器锁)自己理解的他为啥存在
    线程了解以及创建线程的Threading模块中的部分方法
    进程 >> 互斥锁、队列与管道、生产者消费者模型
    进程比较基础的内容
    基于UDP协议的socket套接字编程 基于socketserver实现并发的socket编程
    网络基础 + 简易服务端和客户端
    单例模式
  • 原文地址:https://www.cnblogs.com/zhexuejun/p/14837110.html
Copyright © 2011-2022 走看看