HTTP的DELETE方法Body传递参数问题解决

理论上,Delete method是通过url传递参数的,如果使用body传递参数呢?

前提:

使用HttpClient发送http请求

问题:

httpDelete对象木有setEntity方法

解决方案:覆盖HttpEntityEnclosingRequestBase,重新实现一个HttpDelete类

源码如下:

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

import java.net.URI;

/**
 * Description: HttpDelete 使用 body 传递参数
 * 参考:https://stackoverflow.com/questions/3773338/httpdelete-with-body
 * <p/>
 * User: lishaohua
 * Date: 2017/11/29  12:58
 */
@NotThreadSafe
public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {

    public static final String METHOD_NAME = "DELETE";

    /**
     * 获取方法(必须重载)
     *
     * @return
     */
    @Override
    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();
    }
}

该方法是参考HttpClient的HttpPost类实现的,查看HttpPost的源码:

import java.net.URI;

import org.apache.http.annotation.NotThreadSafe;

/**
 * HTTP POST method.
 * <p>
 * The HTTP POST method is defined in section 9.5 of
 * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC2616</a>:
 * <blockquote>
 * The POST method is used to request that the origin server accept the entity
 * enclosed in the request as a new subordinate of the resource identified by
 * the Request-URI in the Request-Line. POST is designed to allow a uniform
 * method to cover the following functions:
 * <ul>
 *   <li>Annotation of existing resources</li>
 *   <li>Posting a message to a bulletin board, newsgroup, mailing list, or
 *     similar group of articles</li>
 *   <li>Providing a block of data, such as the result of submitting a form,
 *     to a data-handling process</li>
 *   <li>Extending a database through an append operation</li>
 * </ul>
 * </blockquote>
 * </p>
 *
 * @since 4.0
 */
@NotThreadSafe
public class HttpPost extends HttpEntityEnclosingRequestBase {

    public final static String METHOD_NAME = "POST";

    public HttpPost() {
        super();
    }

    public HttpPost(final URI uri) {
        super();
        setURI(uri);
    }

    /**
     * @throws IllegalArgumentException if the uri is invalid.
     */
    public HttpPost(final String uri) {
        super();
        setURI(URI.create(uri));
    }

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

}

没错,就是原封不动抄的!!!

如何使用?

很简单,替换原来的构造方法:

/**
         * 02、构建HttpDelete对象
         */
        //被抛弃的HttpDelete,因为不支持body传递参数
        //HttpDelete httpDelete = new HttpDelete(proxyURL);
        //使用我们重载的HttpDelete
        HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(proxyURL);
        // 处理请求协议
        httpDelete.setProtocolVersion(super.doProtocol(servletRequest));
        // 处理请求头
        httpDelete.setHeaders(super.doHeaders(servletRequest));
        // 处理请求体
        try {
            InputStream inputStream = servletRequest.getInputStream();
            httpDelete.setEntity(new InputStreamEntity(inputStream));

            /*StringBuilder stringBuilder = new StringBuilder();
            BufferedReader bufferedReader = null;
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            }else {
                stringBuilder.append("");
            }
            logger.info("request params---------->"+stringBuilder.toString());
            httpDelete.setEntity(new StringEntity(stringBuilder.toString(), ContentType.APPLICATION_JSON));*/
        } catch (IOException e) {
            logger.error("set httpDelete entity fail", e);
            // 取流失败,则要抛出异常,阻止本次请求
            throw new RuntimeException(e);
        }

        logger.info("DELETE method handler end...");

 

参考文章

https://stackoverflow.com/questions/3773338/httpdelete-with-body

https://www.cnblogs.com/heyus/p/3790234.html

转载于:https://www.cnblogs.com/huahua035/p/7922108.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值