Java | httpClient请求put方法

时间:2025-02-16 10:49:36

概述

目前很多公司项目大多是微服务开发,接口规范一般会采用restful格式,所以请求方式不局限于post和get,还有put和delete。
所以在用httpClient请求的时候,会遇到请求方式的问题,因为非微服务项目只有post和get两种请求方。所以这里做了一个简单的封装。

主要采用httpPosthttpPut的抽象父类 HttpEntityEnclosingRequestBase 进行封装。


支持post put分别对应的 K-V、json方式:

/**
     * 发送 POST 请求(HTTP),K-V形式
     */
    public static String doPostByMap(String apiUrl, Map<String, Object> params, String cookie) {
        return doXwwwFormUrlencoded(apiUrl,params,cookie,new HttpPost(apiUrl));
    }

    /**
     * 发送 PUT 请求(HTTP),K-V形式
     */
    public static String doPutByMap(String apiUrl, Map<String, Object> params, String cookie) {
        return doXwwwFormUrlencoded(apiUrl,params,cookie,new HttpPut(apiUrl));
    }


    /**
     *
     * 发送 POST 请求(HTTP),json形式
     */
    public static String postJson(String url, String params, String cookie) throws IOException {
        return doApplicationJson(url, params,cookie,new HttpPost(url));
    }

    /**
     *
     * 发送 PUT 请求(HTTP),json形式
     */

    public static String putJson(String url, String params, String cookie) throws IOException {
        return doApplicationJson(url, params,cookie,new HttpPut(url));
    }

参数为 K-V 形式:

 public static String doXwwwFormUrlencoded(String apiUrl, Map<String, Object> params, String cookie,HttpEntityEnclosingRequestBase httpRequest){

        String httpStr = null;
        CloseableHttpResponse response = null;

        try {
            List<NameValuePair> pairList = new ArrayList<NameValuePair>(());
            for (<String, Object> entry : ()) {
                NameValuePair pair = new BasicNameValuePair((), ().toString());
                (pair);
            }
            if ((cookie)) {
                ("Cookie", SystemConstant.IYB_LOGIN_COOKIE_KEY + "=" + cookie);
            }
            (new UrlEncodedFormEntity(pairList, ("UTF-8")));
            response = getHttpClient(apiUrl).execute(httpRequest, ());
            (());
            HttpEntity entity = ();
            httpStr = (entity, "UTF-8");
        } catch (IOException e) {
            ();
        } finally {
            if (response != null) {
                try {
                    (());
                } catch (IOException e) {
                    ();
                }
            }
        }
        return httpStr;
    }

参数为 json 形式:

 private static String doApplicationJson(String url, String params,String cookie,HttpEntityEnclosingRequestBase httpRequest) {
        initConfig(httpRequest);
        CloseableHttpResponse response = null;
        try {
            ("url:{}, params: {}", url, params);

            HttpEntity httpEntity = new StringEntity(params, ContentType.APPLICATION_JSON);
            (httpEntity);
            if ((cookie)) {
                ("Cookie", SystemConstant.IYB_LOGIN_COOKIE_KEY + "=" + cookie);
            }
            response = getHttpClient(url).execute(httpRequest, ());
            HttpEntity entity = ();
            String result = (entity, "utf-8");
            (entity);
            return result;
        } catch (Exception e) {
            (());
        } finally {
            try {
                if (response != null)
                    ();
            } catch (Exception e) {
                (());
            }
        }
        return null;
    }