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 ...
随机推荐
- vue interceptors 设置请求头
在main.js添加过滤器,可以 Vue.http.interceptors.push((request,next)=>{ //request.credentials = true; // 接口 ...
- java生成字母首位8位随机码
public String getRedomchar(){ String[] char1 = new String[] { "a", "b", "c& ...
- c语言字符函数
函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include <stdi ...
- Golang常用数据结构(对照python)
python golang init get set extend/update find index size loop list list l := list.New() l.PushBack ...
- (转) tcpdump参数解析及使用详解
tcpdump介绍 原文:http://blog.csdn.net/hzhsan/article/details/43445787 tcpdump 是一个运行在命令行下的抓包工具.它允许用户拦截和显示 ...
- 案例52-crm练习新增客户中加入文件上传功能(struts2文件上传)
1 jsp/customer/add.jsp 完整代码: <%@ page language="java" contentType="text/html; char ...
- Homebrew 配置
使用ruby脚本安装完成homebrew之后, 需要配置三个源以及添加一些环境变量 1. export HOMEBREW_NO_AUTO_UPDATE=true # 不自动检查更新 2. cd $(b ...
- VCL
vcl常用配置 不缓存摸一个资源 在vcl_recv中 if (req.url ~ "private") { return (pass); } 动静分离 先定一个多个backend ...
- C语言中的重定向输入
所谓重定向输入,就是不用从键盘一组一组的输入数据,而是保存为一个文件,直接将该程序的测试数据进行输入即可:使用freopen()函数会将标准输入stdin重定向到文件input.txt(这个文件名自己 ...
- 网站架构:消息队列 Java后端架构
2017-01-13 一.消息队列概述 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题.实现高性能,高可用,可伸缩和最终一致性架构.是大型分布式系统不可缺少的中间 ...