分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

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

发布时间:2023-09-06 01:27责任编辑:沈小雨关键词:暂无标签

理论上,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 */@NotThreadSafepublic 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 */@NotThreadSafepublic 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

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

原文地址:http://www.cnblogs.com/huahua035/p/7922108.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved