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;
        }
    
    }
  • 相关阅读:
    machine learning(11) -- classification: advanced optimization 去求cost function最小值的方法
    machine learning(10) -- classification:logistic regression cost function 和 使用 gradient descent to minimize cost function
    machine learning(9) -- classification:Decision boundary
    machine learning(8) -- classification
    day 23 对象的名称空间 类,对象属性和方法 封装 接口提供
    day 22 面向对象 类与对象 名称空间操作
    day 21 内存管理,正则
    day 14 三元运算符,列表字典推导式,递归,匿名函数,内置函数(排序,映射,过滤,合并)
    day 17 项目开发常用模块
    前端之jQuery
  • 原文地址:https://www.cnblogs.com/zhexuejun/p/14837110.html
Copyright © 2011-2022 走看看