理论上,Delete method是通过url传递参数的,如果使用body传递参数呢?
前提:
使用HttpClient发送http请求
问题:
httpDelete对象木有setEntity方法
解决方案:覆盖HttpEntityEnclosingRequestBase,重新实现一个HttpDelete类
源码如下:
importorg.apache.http.annotation.NotThreadSafe;importorg.apache.http.client.methods.HttpEntityEnclosingRequestBase;importjava.net.URI;/*** Description: HttpDelete 使用 body 传递参数
* 参考:https://stackoverflow.com/questions/3773338/httpdelete-with-body*
* User: lishaohua
* Date: 2017/11/29 12:58*/@NotThreadSafepublic class HttpDeleteWithBody extendsHttpEntityEnclosingRequestBase {public static final String METHOD_NAME = "DELETE";/*** 获取方法(必须重载)
*
*@return
*/@OverridepublicString getMethod() {returnMETHOD_NAME;
}public HttpDeleteWithBody(finalString uri) {super();
setURI(URI.create(uri));
}public HttpDeleteWithBody(finalURI uri) {super();
setURI(uri);
}publicHttpDeleteWithBody() {super();
}
}
该方法是参考HttpClient的HttpPost类实现的,查看HttpPost的源码:
importjava.net.URI;importorg.apache.http.annotation.NotThreadSafe;/*** HTTP POST method.
*
* The HTTP POST method is defined in section 9.5 of
* RFC2616:
*
* 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:
*
*
Annotation of existing resources*
Posting a message to a bulletin board, newsgroup, mailing list, or* similar group of articles
*
Providing a block of data, such as the result of submitting a form,* to a data-handling process
*
Extending a database through an append operation*
*
*
*
*@since4.0*/@NotThreadSafepublic class HttpPost extendsHttpEntityEnclosingRequestBase {public final static String METHOD_NAME = "POST";publicHttpPost() {super();
}public HttpPost(finalURI uri) {super();
setURI(uri);
}/***@throwsIllegalArgumentException if the uri is invalid.*/
public HttpPost(finalString uri) {super();
setURI(URI.create(uri));
}
@OverridepublicString getMethod() {returnMETHOD_NAME;
}
}
没错,就是原封不动抄的!!!
如何使用?
很简单,替换原来的构造方法:
/*** 02、构建HttpDelete对象*/
//被抛弃的HttpDelete,因为不支持body传递参数//HttpDelete httpDelete = new HttpDelete(proxyURL);//使用我们重载的HttpDelete
HttpDeleteWithBody httpDelete = newHttpDeleteWithBody(proxyURL);//处理请求协议
httpDelete.setProtocolVersion(super.doProtocol(servletRequest));//处理请求头
httpDelete.setHeaders(super.doHeaders(servletRequest));//处理请求体
try{
InputStream inputStream=servletRequest.getInputStream();
httpDelete.setEntity(newInputStreamEntity(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 newRuntimeException(e);
}
logger.info("DELETE method handler end...");
参考文章
https://stackoverflow.com/questions/3773338/httpdelete-with-body
https://www.cnblogs.com/heyus/p/3790234.html