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 ...
随机推荐
- 配置WAMP完美攻略
软件介绍 Wamp Server 是一款功能强大的PHP 集成安装环境. 为了节约时间,本次使用 Wamp Server 来进行配置. wamp 的全部含义就是本篇文章的标题. 使用版本和操作系统 W ...
- IE7+ 浏览器兼容预览本地图片
css #preview_fake { filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale); o ...
- AUTO Uninstaller 下载 (maya/3dsmax/cad/Inventor/Revit uninstall tool 卸载修复工具)
小伙伴是不是遇到 MAYA/CAD/3DSMAX/INVENTOR/REVIT 安装失败或者安装不了的问题了呢?AUTODESK系列软件着实令人头疼,MAYA/CAD/3DSMAX/INVENTOR/ ...
- [转]jQuery TextBox Water Mark with asp.net
本文转自:http://naspinski.net/post/jQuery-TextBox-Water-Mark-with-aspnet.aspx I stole majority of this c ...
- 通过rsync+inotify实现数据的实时备份(多台备份机)
在前面的博文中,我讲到过利用rsync实现数据的镜像和备份,但是要实现数据的实时备份,单独靠rsync还不能实现,本文就讲述下如何实现数据的实时备份. 一.rsync的优点与不足 与传统的cp.ta ...
- 37、解决 HTMLTestRunner 中文显示乱码的问题
1.在自己的测试脚本中加入下面的代码并保存: # -.- coding:utf-8 -.- import sys reload(sys) sys.setdefaultencoding('utf-8') ...
- php乱码的解决方法
一般如果会出现乱码,基本都是因为编码不统一造成的,所以我们只需要把编码改成一样的就好了. 我们编码一般都是以utf-8为主: 文件编码都是UTF-8无BOM,如果文件不是这个编码,可以使用文件编辑器转 ...
- HDU 4342——History repeat itself——————【数学规律】
History repeat itself Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. O ...
- HDU 5375——Gray code——————【dp||讨论】
Gray code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- Sql 本周当天本期日期转换
--查询当天: --查询24小时内的: --info为表名,datetime为数据库中的字段值 --查询当天: --查询24小时内的: select * from table where DateDi ...