一般 HttpPost 对传参 Json 的处理是:
// 中文处理
StringEntity se = new StringEntity(json, Consts.UTF_8); httppost.setEntity(se);
HttpPost
、HttpPut
继承了HttpEntityEnclosingRequestBase
类,所以有setEntity
方法。详情请自行查看源码。
而HttpDelete
没有对应的setEntity()方法,那么怎么传递呢?
在网上找到了一种解决办法:
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import java.net.URI; import org.apache.http.annotation.NotThreadSafe; @NotThreadSafe public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase { public static final String METHOD_NAME = "DELETE"; 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(); } }
然后就可以直接调用了,调用方式如下;
HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url); // json 处理 httpdelete.setHeader("Content-Type", "application/json; charset=UTF-8");//or addHeader(); httpdelete.setHeader("X-Requested-With", "XMLHttpRequest"); //设置HttpDelete的请求参数 httpdelete.setEntity(new StringEntity(paramsJsonStr)); httpDelete.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); httpdelete.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); HttpResponse response = client.execute(httpdelete);
HttpClient处理代码示例如下
HttpMethod.java
public enum HttpMethod { Get, Post, Put, DELETE; }
HttpDeleteWithBody.java
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import java.net.URI; /** * Created by liurenkui on 2017/10/13. */ public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase { public static final String METHOD_NAME = "DELETE"; public HttpDeleteWithBody(final String uri) { super(); setURI(URI.create(uri)); } public HttpDeleteWithBody() { super(); } @Override public String getMethod() { return METHOD_NAME; } }
请求方法封装
public static String httpRequest(String requestUrl, String requestJson, HttpMethod httpMethod) { CloseableHttpClient httpClient = null; try { httpClient = HttpClients.createDefault(); HttpResponse response; if (null == httpMethod) { throw new RuntimeException("Http Method should be Get, Post, Put or Delete."); } if (HttpMethod.Get == httpMethod) { HttpGet httpGet = new HttpGet(requestUrl); response = httpClient.execute(httpGet); } else { HttpEntityEnclosingRequestBase requestBase = null; switch (httpMethod) { case Post: requestBase = new HttpPost(requestUrl); break; case Put: requestBase = new HttpPut(requestUrl); break; case DELETE: requestBase = new HttpDeleteWithBody(requestUrl); break; } if (StringUtils.isNotBlank(requestJson)) { StringEntity requestEntity = new StringEntity(requestJson, ContentType.APPLICATION_JSON); if (requestBase != null) { requestBase.setEntity(requestEntity); } } response = httpClient.execute(requestBase); } HttpEntity httpEntity = response.getEntity(); return EntityUtils.toString(httpEntity); } catch (IOException e) { throw new RuntimeException(e); } finally { try { if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } }
请求示例
HttpClient.httpRequest( "http://localhost:8081/firefly/inner/seekingLawyerRequirement", "{\"id\":\"" + id + "\"}", HttpMethod.DELETE)