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
HTTP的DELETE方法Body传递参数问题解决的更多相关文章
- 工作随笔——Java调用Groovy类的方法、传递参数和获取返回值
接触Groovy也快一年了,一直在尝试怎么将Groovy引用到日常工作中来.最近在做一个功能的时候,花了点时间重新看了下Java怎么调用Groovy的方法.传递参数和获取返回值. 示例Groovy代码 ...
- Groovy小结:java调用Groovy方法并传递参数
Groovy小结:java调用Groovy方法并传递参数 @(JAVA总结) 1. 场景描述 在网上查了资料发现,java有三种方式调用groovy脚本.但是真正在实际的服务器环境中,嵌入groovy ...
- 利用Ajax调用controller方法并传递参数
一.背景由于近期工作需要将人脸识别功能与选课系统结合,但是对前端知识了解的很少,只能边做边学了,因此在这边把遇到的一些坑说明一下,希望能帮助到像我一样的初学者 二.具体内容这里采用框架为MVC,如果想 ...
- struts 页面调用Action的指定方法并传递参数
如果为action配置了类,那么默认就会执行Action类的excute方法,Action类的写法三种: ① public class Action1 { public String execute( ...
- odoo14 button 事件调用python方法如何传递参数
1 <field name="user_ids" 2 mode="kanban" 3 nolabel="1" 4 options=&q ...
- jquery引用方法时传递参数
经常到网上去下载大牛们写的js插件.每次只需将js引用并设置下变量就行了,但一直没搞明白原理(主要是大牛们的代码太简练了-,-). 这次弄清了如何传递.设置多个(很多个)参数. 如 方法为functi ...
- laravel 视图调用方法并传递参数
视图层 route 中文 路由 <a href="{{route('cc',array('id'=>11111))}}">446454</a> 路由层 ...
- Java学习——方法中传递参数分简单类型与复杂类型(引用类型)编程计算100+98+96+。。。+4+2+1的值,用递归方法实现
package hello; public class digui { public static void main(String[] args) { // TODO Auto-generated ...
- 利用GetType反射方法再调用方法进行传递参数实现调用
直接上代码: TestMenuService.MenuServiceCSClient tesClient = new TestMenuService.MenuServiceCSClient(); va ...
随机推荐
- equals()方法 与 == 区别
equals()方法 与 == 区别 : equals():在字符串中比较内容,则必须使用equals(变量),如果相等则为true,否则为false == :使用==比较的是地址是否一致 class ...
- public class 与 class 的区别
public class 与 class 的区别 1.一个类前面的public是可有可无的 2.如果一个类使用 public 修饰,则文件名必须与类名一致 3.如果一个类前面没有使用public修饰, ...
- leetcode 196. Delete Duplicate Emails 配合查询的delete
https://leetcode.com/problems/delete-duplicate-emails/description/ 题意要对原来的数据表进行删除,不删除不行,它每次只输出原来那个表. ...
- 软件使用---Eclipse
代码提示快捷操作.这个叫做,内容分析(content assist) 1.设置自动提示: 2.设置快捷键:
- cout和printf不能混用
1.两者的缓存机制不同:printf无缓冲区,而std::cout有 (其实printf也是有缓冲区的,https://blog.csdn.net/ithzhang/article/details/6 ...
- HDU 4364——Matrix operation——————【模拟题】
Matrix operation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- ssh设置别名
通常我们在 Termianl 下用 ssh 链接远程主机的时候,每次都需要输入一长串的用户名加主机地址,是不是觉得很麻烦? 我们知道在 /etc/ssh/ 目录下通常都会有 ssh_config 和 ...
- 【安全测试】sql注入
SQL注入攻击是黑客对 数据库 进行攻击的常用手段之一,随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员越来越多,但是由于程序员水平及经验页参差不齐,相当大部分程序员在编写代码的时候没有 ...
- Java学习笔记--继承和多态(中)
1.通过继承来开发超类(superclass) 2.使用super 关键词唤起超类的构造方法 3.在超类中覆盖方法 4.区分override和overload 5.在Object类中探索toStrin ...
- 方法引用(Method reference)和构造器引用(construct reference)
Java 8 允许你使用 :: 关键字来传递方法或者构造函数引用 方法引用语法格式有以下三种: objectName::instanceMethod ClassName::staticMethod C ...