HttpClient的HttpDelete请求方式传递参数

一般 HttpPost 对传参 Json 的处理是:

// 中文处理

StringEntity se = new StringEntity(json, Consts.UTF_8);
httppost.setEntity(se);

HttpPostHttpPut继承了HttpEntityEnclosingRequestBase类,所以有setEntity方法。详情请自行查看源码。

HttpDelete没有对应的setEntity()方法,那么怎么传递呢?

在网上找到了一种解决办法:

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import java.net.URI;
import org.apache.http.annotation.NotThreadSafe;
@NotThreadSafe
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 httpDelete = new HttpDeleteWithBody(url);
 
// json 处理
httpdelete.setHeader("Content-Type", "application/json; charset=UTF-8");//or addHeader();
httpdelete.setHeader("X-Requested-With", "XMLHttpRequest");
//设置HttpDelete的请求参数
httpdelete.setEntity(new StringEntity(paramsJsonStr));
httpDelete.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
httpdelete.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);
HttpResponse response = client.execute(httpdelete);

HttpClient处理代码示例如下

HttpMethod.java

public enum HttpMethod {
    Get, Post, Put, DELETE;
}

HttpDeleteWithBody.java

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

import java.net.URI;

/**
 * Created by liurenkui on 2017/10/13.
 */
public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
    public static final String METHOD_NAME = "DELETE";

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

    public HttpDeleteWithBody() {
        super();
    }

    @Override
    public String getMethod() {
        return METHOD_NAME;
    }
}

请求方法封装

public static String httpRequest(String requestUrl, String requestJson, HttpMethod httpMethod) {
    CloseableHttpClient httpClient = null;
    try {
        httpClient = HttpClients.createDefault();
        HttpResponse response;

        if (null == httpMethod) {
            throw new RuntimeException("Http Method should be Get, Post, Put or Delete.");
        }

        if (HttpMethod.Get == httpMethod) {
            HttpGet httpGet = new HttpGet(requestUrl);
            response = httpClient.execute(httpGet);
        } else {
            HttpEntityEnclosingRequestBase requestBase = null;

            switch (httpMethod) {
                case Post:
                    requestBase = new HttpPost(requestUrl);
                    break;
                case Put:
                    requestBase = new HttpPut(requestUrl);
                    break;
                case DELETE:
                    requestBase = new HttpDeleteWithBody(requestUrl);
                    break;
            }

            if (StringUtils.isNotBlank(requestJson)) {
                StringEntity requestEntity = new StringEntity(requestJson, ContentType.APPLICATION_JSON);
                if (requestBase != null) {
                    requestBase.setEntity(requestEntity);
                }
            }

            response = httpClient.execute(requestBase);
        }

        HttpEntity httpEntity = response.getEntity();
        return EntityUtils.toString(httpEntity);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (httpClient != null) {
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

请求示例

HttpClient.httpRequest( "http://localhost:8081/firefly/inner/seekingLawyerRequirement", "{\"id\":\"" + id + "\"}", HttpMethod.DELETE)





赞(52) 打赏
未经允许不得转载:优客志 » JAVA开发
分享到:

相关推荐

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏