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 ...
随机推荐
- pthread_cond_wait函数的学习以及其他
pthread_cond_wait() 前使用 while 讲解2009-10-27 9:33LINUX环境下多线程编程肯定会遇到需要条件变量的情况,此时必然要使用pthread_cond_wait( ...
- SpringBoot---数据缓存(未完待续)
1.概述 1.1 在Spring中使用缓存的关键是配置CacheManager,而SpringBoot为我们自动配置了多个CacheManager的实现: 1.2 SpringBoot的CacheMa ...
- 查看CPU和内存,用机器指令和汇编指令编程【Debug模式】
命令 作用 举例 R 查看,改变CPU寄存器的内容 查看:r 改写:r ax D 查看内存中的内容 d 1000:0 f E 改写内存中的内容 e 1000:0 f U 将内存中的机器指令翻译成汇编指 ...
- Django-3 视图层
5.1 视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片 ...
- vue进阶 --- 实例演示
这篇博客将通过一个实例来对vue构建项目的过程有一个了解. 主要用到的知识点如下所示: vue-router 2.0路由配置 router-view 和 router-link的使用 transiti ...
- Andrew Ng 的 Machine Learning 课程学习 (week2) Linear Regression
这学期一直在跟进 Coursera上的 Machina Learning 公开课, 老师Andrew Ng是coursera的创始人之一,Machine Learning方面的大牛.这门课程对想要了解 ...
- #include stdio.h(5)
#include <stdio.h> int main() { //1.数组的排序-冒泡排序 /* 1.规则:相邻的两个数据进行比较 2.如果有N个数据,需要选择N-1次参照物来比较, 因 ...
- 跨平台图表控件TeeChart使用教程:将图表数据导出为XML格式
在开发者使用TeeChart进行开发的过程中,不管是在设计时或者运行时都可以使用的图表导出对话框将图表数据轻易地导出为XML格式: TeeChart最新版那下载地址 上图为TeeChart导出对话框的 ...
- python3线程介绍01(如何启动和调用线程)
#!/usr/bin/env python# -*- coding:utf-8 -*- import osimport time,randomimport threading # 1-进程说明# 进程 ...
- JAVA StringBuffer的用法
在使用StringBuffer 的时候,习惯性的像String一样把他初始化了 StringBuffer result = null; 结果警告:Null pointer access: The va ...