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;
        }
    
    }
  • 相关阅读:
    java.lang.UnsupportedOperationException: Not supported by BasicDataSource
    c# seo 百度sitemap书写
    c# 泛型原理(旧)
    apache 服务器配置常用知识点合集
    sass 基本常识
    c# TryParse
    webpack 配置热更新
    c# ref和out 详解
    IIS applicationHost.config 查找历史
    c# webapi 自定义返回数据
  • 原文地址:https://www.cnblogs.com/zhexuejun/p/14837110.html
Copyright © 2011-2022 走看看