java积累---HttpDelete请求方式传递参数

本文详细介绍了如何在HTTP Delete请求中附带Body内容,通过自定义HttpDeleteWithBody类继承HttpEntityEnclosingRequestBase,实现了setEntity方法,从而能够发送包含JSON数据的Delete请求。文章提供了完整的代码示例,包括设置请求头、超时时间和执行请求的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

// 中文处理

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

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

查看httpclient-4.2.3的源码可以发现,methods包下面包含HttpGet, HttpPost, HttpPut, HttpDelete等类来实现http的常用操作。

其中,HttpPost继承自 HttpEntityEnclosingRequestBase,HttpEntityEnclosingRequestBase类又实现了 HttpEntityEnclosingRequest接口,实现了setEntity的方法。

而HttpDelete继承自HttpRequestBase,没有实现setEntity的方法,因此无法设置HttpEntity对象。

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

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

package net.dn.client;
 
import java.io.IOException;
import java.net.URI;
import org.apache.http.Header;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.entity.ContentType;
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;
 
 
@NotThreadSafe
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();
    }
}
 
public class ClientMain {
    public static void main(String[] args) throws ClientProtocolException, IOException {
 
        CloseableHttpClient httpclient = HttpClients.createDefault();
 
        String url = "http://localhost:8080/RESTfulDeleteWithBody/rest";
 
        HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);
        StringEntity input = new StringEntity("John Dow", ContentType.APPLICATION_JSON);
         
        httpDelete.setEntity(input);  
 
        System.out.println("****REQUEST***************************************");
        System.out.println(url);
        Header requestHeaders[] = httpDelete.getAllHeaders();
        for (Header h : requestHeaders) {
            System.out.println(h.getName() + ": " + h.getValue());
        }
 
        CloseableHttpResponse response = httpclient.execute(httpDelete);
 
        System.out.println("****RESPONSE***************************************");
        System.out.println("----status:---------------------");
        System.out.println(response.getStatusLine());
        System.out.println("----header:---------------------");
        Header responseHeaders[] = response.getAllHeaders();
        for (Header h : responseHeaders) {
            System.out.println(h.getName() + ": " + h.getValue());
        }
        System.out.println("----content:---------------------");
        System.out.println(EntityUtils.toString(response.getEntity()));
    }
}

然后就可以直接调用了,调用方式如下;

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);

参考链接:
https://daweini.wordpress.com/2013/12/20/apache-httpclient-send-entity-body-in-a-http-delete-request/

https://www.cnblogs.com/xgjblog/p/3918956.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿泽财商会

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

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

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

打赏作者

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

抵扣说明:

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

余额充值