如何使 HttpClient 的 Delete 支持 HttpEntity 传参

pom 依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
</dependency>

问题所在

HttpDelete 继承自 HttpRequestBase,而 HttpRequestBase 中并没有关于设置 HttpEntity 的实现。

解决方案

我们知道 HttpPost 是可以设置 HttpEntity 的,通过其源码实现可以看到 HttpPost 继承自 HttpEntityEnclosingRequestBase。而正是 HttpEntityEnclosingRequestBase 提供了设置 HttpEntity 的实现。

如何使 HttpClient 的 Delete 支持 HttpEntity 传参

那我们的解决方案就是模拟HttpPost实现一个我们自己的HttpDelete,如下:

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;

import java.net.URI;

public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
    public static final String METHOD_NAME = "DELETE";
    public String getMethod() { return METHOD_NAME; }

    public HttpDeleteWithBody(final String uri) {
        super();
        setURI(URI.create(uri));
    }
    public HttpDeleteWithBody(final URI uri) {
        super();
        setURI(uri);
    }
    public HttpDeleteWithBody() { super(); }
}

调用HttpDeleteWithBody示例

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class HttpClient {

    private static final int TIMEOUT = 30 * 1000;

    private static final String UTF8 = "UTF-8";

    private static final String CONTENT_TYPE_JSON = "application/json";

    private HttpClient() {
    }

    private static String sendHttpMethod(HttpRequestBase httpMethod) throws IOException {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        String responseContent = null;
        httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(TIMEOUT).setSocketTimeout(TIMEOUT)
                .setConnectionRequestTimeout(TIMEOUT).build();
        httpMethod.setConfig(requestConfig);
        response = httpClient.execute(httpMethod);
        entity = response.getEntity();
        responseContent = EntityUtils.toString(entity, UTF8);
        EntityUtils.consume(entity);
        if (response != null) {
            response.close();
        }
        if (httpClient != null) {
            httpClient.close();
        }
        return responseContent;
    }

    public static Map<String, Object> sendHttpDelete(String url, String jsonStr) throws IOException {

        Map<String, Object> results = new HashMap<>();
		
        HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);
        httpDelete.addHeader("content-type", CONTENT_TYPE_JSON);
		
        StringEntity entity = new StringEntity(jsonStr, UTF8);
        entity.setContentEncoding(UTF8);
        entity.setContentType(CONTENT_TYPE_JSON);
        httpDelete.setEntity(new StringEntity(jsonStr, UTF8));

        String stringRet = sendHttpMethod(httpDelete);
        if (StringUtils.isNotBlank(stringRet)) {
            ObjectMapper objectMapper = new ObjectMapper();
            results = objectMapper.readValue(stringRet, Map.class);

        }
        return results;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

i余数

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值