apache HttpClient

简单备注下Apache HttpClient常用封装使用包括get,post及发送文件相关,直接上代码。

public static void closeHttpResource(CloseableHttpClient closeableHttpClient,
                                         CloseableHttpResponse closeableHttpResponse) {
        try {
            if (null != closeableHttpClient) {
                closeableHttpClient.close();
            }
        } catch (Exception e) {
            LOGGER.warn("closeableHttpClient error {}", e);
        }
        try {
            if (null != closeableHttpResponse) {
                closeableHttpResponse.close();
            }
        } catch (Exception e) {
            LOGGER.warn("closeableHttpResponse error {}", e);
        }
    }

    public static CloseableHttpResponse sendHttpGet(CloseableHttpClient httpClient, String url, Map<String, String> headers) throws Exception {
        try {
            HttpGet httpGet = new HttpGet(url);
            httpGet.setConfig(config);
            headers.forEach(httpGet::addHeader);
            return httpClient.execute(httpGet);
        } catch (Exception e) {
            throw e;
        }
    }

    public static CloseableHttpResponse sendHttpGet(CloseableHttpClient httpClient, String url, Map<String, String> params, Map<String, String> headers) throws Exception {
        try {
            List<NameValuePair> list = Lists.newArrayList();
            params.forEach((k, v) -> list.add(new BasicNameValuePair(k, v)));
            URIBuilder uriBuilder = new URIBuilder(url).addParameters(list);
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            httpGet.setConfig(config);
            headers.forEach(httpGet::addHeader);
            return httpClient.execute(httpGet);
        } catch (Exception e) {
            throw e;
        }
    }

    public static ApiResponse<String> getResponse(CloseableHttpResponse response, String charset) throws IOException {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        if (response.getStatusLine().getStatusCode() == Constants.HTTP_OK) {
            apiResponse.setData(EntityUtils.toString(response.getEntity(), charset));
            return apiResponse;
        }
        return new ApiResponse<>(response.getStatusLine().getStatusCode(), "网络异常");
    }

    public static CloseableHttpResponse sendHttpPost(CloseableHttpClient httpClient, String url, Map<String, String> headers) throws Exception {
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(config);
            headers.forEach(httpPost::addHeader);
            return httpClient.execute(httpPost);
        } catch (Exception e) {
            throw e;
        }
    }

    public static CloseableHttpResponse sendHttpPost(CloseableHttpClient httpClient, String url, Map<String, String> params, Map<String, String> headers) throws Exception {
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(config);
            headers.forEach(httpPost::addHeader);
            List<NameValuePair> list = Lists.newArrayList();
            params.forEach((k, v) -> list.add(new BasicNameValuePair(k, v)));
            UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(list);
            httpPost.setEntity(encodedFormEntity);
            return httpClient.execute(httpPost);
        } catch (Exception e) {
            throw e;
        }
    }

    public static CloseableHttpResponse sendHttpPost(CloseableHttpClient httpClient, String url, Map<String, String> params, Map<String, String> headers, List<MultipartFile> files) throws Exception {
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(config);
            headers.forEach(httpPost::addHeader);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setCharset(Charset.forName(Constants.DEFAULT_CHARSET));
            int idx = 1;
            for (MultipartFile file : files) {
                builder.addBinaryBody("file".concat(String.valueOf(idx)), file.getInputStream(), ContentType.MULTIPART_FORM_DATA, file.getName());
                idx++;
            }
            params.forEach(builder::addTextBody);
            httpPost.setEntity(builder.build());
            return httpClient.execute(httpPost);
        } catch (Exception e) {
            throw e;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值