HttpClient发送Get和表单或json方式的Post使用方法

  • Post author:
  • Post category:其他

功能描述

  1. 简单的get请求
  2. 简单的post请求
  3. post发送json字符串
  4. post请求发送字符串与MultipartFile文件

代码块

  1. 引入pom,此处为gradle引入
        compile 'org.apache.httpcomponents:httpclient:4.5.2'
        compile 'org.apache.httpcomponents:httpmime:4.5.2'
    
  2. 工具类代码
    @Slf4j
    public class HttpClientUtil {
    
        /**
         * @Title: doGet
         * @Description: get方式
         * @param url 请求路径
         * @param params 参数
         */
        public static String doGet(String url, Map<String, String> params) {
    
            // 返回结果
            String result = "";
            // 创建HttpClient对象
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpGet httpGet = null;
            try {
                // 拼接参数,可以用URIBuilder,也可以直接拼接在?传值,拼在url后面,如下--httpGet = new
                // HttpGet(uri+"?id=123");
                URIBuilder uriBuilder = new URIBuilder(url);
                if (null != params && !params.isEmpty()) {
                    for (Map.Entry<String, String> entry : params.entrySet()) {
                        uriBuilder.addParameter(entry.getKey(), entry.getValue());
                        // 或者用
                        // 顺便说一下不同(setParameter会覆盖同名参数的值,addParameter则不会)
                        // uriBuilder.setParameter(entry.getKey(), entry.getValue());
                    }
                }
                URI uri = uriBuilder.build();
                // 创建get请求
                httpGet = new HttpGet(uri);
                log.info("访问路径:" + uri);
                HttpResponse response = httpClient.execute(httpGet);
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 返回200,请求成功
                    // 结果返回
                    result = EntityUtils.toString(response.getEntity());
                    log.info("请求成功!,返回数据:" + result);
                } else {
                    log.info("请求成功,但是返回错误码,response:{}",response);
                   //todo 自己处理业务,可以选择抛出自定义异常
                }
            } catch (IllegalArgumentException ie){
                log.error("http请求参数出现异常,ie:{}",ie.getMessage());
                //todo 自己处理业务,可以选择抛出自定义异常
            } catch (HttpHostConnectException he){
                log.error("连接IP超时,he:",he);
                //todo 自己处理业务,可以选择抛出自定义异常
            } catch (Exception e) {
                log.error("请求异常,e:",e);
                //todo 自己处理业务,可以选择抛出自定义异常
            } finally {
                // 释放连接
                if (null != httpGet) {
                    httpGet.releaseConnection();
                }
            }
            return result;
        }
    
        /**
         * @Title: doPost
         * @Description: post请求
         * @param url
         * @param params
         */
        public static String doPost(String url, Map<String, String> params) {
            String result = "";
            // 创建httpclient对象
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = null;
            try { // 参数键值对
                httpPost = new HttpPost(url);
                if (null != params && !params.isEmpty()) {
                    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                    NameValuePair pair = null;
                    for (String key : params.keySet()) {
                        pair = new BasicNameValuePair(key, params.get(key));
                        pairs.add(pair);
                    }
                    // 模拟表单
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);
                    httpPost.setEntity(entity);
                }
                HttpResponse response = httpClient.execute(httpPost);
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    result = EntityUtils.toString(response.getEntity(), "utf-8");
                    log.info("返回数据:>>>" + result);
                } else {
                    log.info("请求成功,但是返回错误码,response:{}",response);
                     //todo 自己处理业务,可以选择抛出自定义异常
                }
            } catch (IllegalArgumentException ie) {
                log.error("http请求参数出现异常,ie:{}", ie.getMessage());
                 //todo 自己处理业务,可以选择抛出自定义异常
            } catch (HttpHostConnectException he){
                log.error("连接IP超时,he:",he);
                 //todo 自己处理业务,可以选择抛出自定义异常
            } catch (Exception e) {
                log.error("请求异常,e:",e);
                 //todo 自己处理业务,可以选择抛出自定义异常
            } finally {
                if (null != httpPost) {
                    // 释放连接
                    httpPost.releaseConnection();
                }
            }
            return result;
        }
    
        /**
         * @Title: sendJsonStr
         * @Description: post发送json字符串
         * @param url
         * @param params
         */
        public static String sendJsonStr(String url, String params) {
            String result = "";
    
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(url);
            try {
                httpPost.addHeader("Content-type", "application/json; charset=utf-8");
                httpPost.setHeader("Accept", "application/json");
                if (StringUtils.isNotBlank(params)) {
                    httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
                }
                HttpResponse response = httpClient.execute(httpPost);
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    result = EntityUtils.toString(response.getEntity());
                    log.info("返回数据:" + result);
                } else {
                    log.info("请求成功,但是返回错误码,response:{}",response);
                     //todo 自己处理业务,可以选择抛出自定义异常
                }
            } catch (IllegalArgumentException ie) {
                log.error("http请求参数出现异常,ie:{}", ie.getMessage());
                 //todo 自己处理业务,可以选择抛出自定义异常
            } catch (HttpHostConnectException he){
                log.error("连接IP超时,he:",he);
                 //todo 自己处理业务,可以选择抛出自定义异常
            } catch (Exception e) {
                log.error("请求异常,e:",e);
                 //todo 自己处理业务,可以选择抛出自定义异常
            }
            return result;
        }
    
        /**
         *//todo 可以把多个参数当成map传进来,保证更好的通用性,因为使用情况不多,所以写死了
         * @Title: doPostFile
         * @Description: post请求
         * @param url url
         * @param param1 请求参数
         * @param param2 请求参数
         * @param file 请求参数文件
         */
        public static String doPostFile(String url,String param1, String param2, MultipartFile file) {
            String result = "";
            // 创建httpclient对象
            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = null;
            try { // 参数键值对
                httpPost = new HttpPost(url);
                // 模拟表单
                InputStream is = new ByteArrayInputStream(file.getBytes());
                InputStreamBody fileBody = new InputStreamBody(is, ContentType.MULTIPART_FORM_DATA,file.getOriginalFilename());
    
                StringBody param1Comment = new StringBody(param1, ContentType.APPLICATION_JSON);
                StringBody param2Comment = new StringBody(param2, ContentType.APPLICATION_JSON);
                HttpEntity reqEntity = MultipartEntityBuilder.create()
                        .addPart("file", fileBody)
                        .addPart("param1 ", param1Comment)
                        .addPart("param2 ", param2Comment)
                        .build();
                httpPost.setEntity(reqEntity);
    
                HttpResponse response = httpClient.execute(httpPost);
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    result = EntityUtils.toString(response.getEntity(), "utf-8");
                    log.info("返回数据:>>>" + result);
                } else {
                    log.info("请求成功,但是返回错误码,response:{}",response);
                     //todo 自己处理业务,可以选择抛出自定义异常
                }
            } catch (IllegalArgumentException ie) {
                log.error("http请求参数出现异常,ie:{}", ie.getMessage());
                 //todo 自己处理业务,可以选择抛出自定义异常
            } catch (HttpHostConnectException he){
                log.error("连接IP超时,he:",he);
                 //todo 自己处理业务,可以选择抛出自定义异常
            } catch (Exception e) {
                log.error("请求异常,e:",e);
                 //todo 自己处理业务,可以选择抛出自定义异常
            } finally {
                if (null != httpPost) {
                    // 释放连接
                    httpPost.releaseConnection();
                }
            }
            return result;
        }
    
    

版权声明:本文为mojiewangday原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。