理论上,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