理论上,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

HTTP的DELETE方法Body传递参数问题解决的更多相关文章

  1. 工作随笔——Java调用Groovy类的方法、传递参数和获取返回值

    接触Groovy也快一年了,一直在尝试怎么将Groovy引用到日常工作中来.最近在做一个功能的时候,花了点时间重新看了下Java怎么调用Groovy的方法.传递参数和获取返回值. 示例Groovy代码 ...

  2. Groovy小结:java调用Groovy方法并传递参数

    Groovy小结:java调用Groovy方法并传递参数 @(JAVA总结) 1. 场景描述 在网上查了资料发现,java有三种方式调用groovy脚本.但是真正在实际的服务器环境中,嵌入groovy ...

  3. 利用Ajax调用controller方法并传递参数

    一.背景由于近期工作需要将人脸识别功能与选课系统结合,但是对前端知识了解的很少,只能边做边学了,因此在这边把遇到的一些坑说明一下,希望能帮助到像我一样的初学者 二.具体内容这里采用框架为MVC,如果想 ...

  4. struts 页面调用Action的指定方法并传递参数

    如果为action配置了类,那么默认就会执行Action类的excute方法,Action类的写法三种: ① public class Action1 { public String execute( ...

  5. odoo14 button 事件调用python方法如何传递参数

    1 <field name="user_ids" 2 mode="kanban" 3 nolabel="1" 4 options=&q ...

  6. jquery引用方法时传递参数

    经常到网上去下载大牛们写的js插件.每次只需将js引用并设置下变量就行了,但一直没搞明白原理(主要是大牛们的代码太简练了-,-). 这次弄清了如何传递.设置多个(很多个)参数. 如 方法为functi ...

  7. laravel 视图调用方法并传递参数

    视图层 route 中文 路由 <a href="{{route('cc',array('id'=>11111))}}">446454</a> 路由层 ...

  8. Java学习——方法中传递参数分简单类型与复杂类型(引用类型)编程计算100+98+96+。。。+4+2+1的值,用递归方法实现

    package hello; public class digui { public static void main(String[] args) { // TODO Auto-generated ...

  9. 利用GetType反射方法再调用方法进行传递参数实现调用

    直接上代码: TestMenuService.MenuServiceCSClient tesClient = new TestMenuService.MenuServiceCSClient(); va ...

随机推荐

  1. Fortran和C的编译器PGI部署

    平台信息 Description: CentOS Linux release 7.6.1810 (Core) 安装步骤 获取PGCC:社区版是免费的,自带license.dat 解压下载的压缩包:ta ...

  2. 不要在Application中缓存数据

    在你的App中的很多地方都需要使用到数据信息,它可能是一个session token,一次费时计算的结果等等,通常为了避免Activity之间传递数据的开销,会将这些数据通过持久化来存储.   有人建 ...

  3. leetcode 196. Delete Duplicate Emails 配合查询的delete

    https://leetcode.com/problems/delete-duplicate-emails/description/ 题意要对原来的数据表进行删除,不删除不行,它每次只输出原来那个表. ...

  4. 性能测试工具Jmeter07-Jmeter性能测试实战

    测试需求:测试20个用户访问www.baozhenart.com在负载达到30QPS时的平均响应时间 QPS:Query Per Second每秒查询率.是一台查询服务器每秒能够处理的查询次数.在因特 ...

  5. 牛客网Java刷题知识点之什么是进程、什么是线程、什么是多线程、多线程的好处和弊端、多线程的创建方式、JVM中的多线程解析、多线程运行图解

    不多说,直接上干货! 什么是进程? 正在进行中的程序(直译). 什么是线程? 就是进程中一个负责程序执行的控制单元(执行路径). 见 牛客网Java刷题知识点之进程和线程的区别 什么是多线程? 一个进 ...

  6. 一个WPF小项目小结

    一:缘起 老板有做PC桌面客户端的需求,做的是能耗的计算和评估,要算能耗,就有很多环节,最后对这些环节数据进行一些简单计算.我想要是做的话就用比较熟的wpf,就去聊了下,对方给了1张比较复杂的Exce ...

  7. C#使用进度条,并用线程模拟真实数据 ProgressBar用法(转)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. ab (ApacheBench)命令

    ab (ApacheBench)命令 参数 -n 在测试会话中所执行的请求个数.默认时,仅执行一个请求 -c 一次产生的请求个数.默认是一次一个 -t 测试所进行的最大秒数 -k 启用HTTP Kee ...

  9. Bootstrap学习笔记(四)

    四.JS插件 概述:与jQueryUI库类似,Bootstrap提供了十几个插件函数.有两种调用方法: (1) 传统JS变成方式 $(...).插件函数.(); (2) 使用data-*扩展属性(推荐 ...

  10. Struts_ActionWildcard_通配符配置

    使用通配符,将配置量降到最低 不过,一定要遵守“约定由于配置”的原则 struts2.xml <?xml version="1.0" encoding="UTF-8 ...