一般 HttpPost 对传参 Json 的处理是:
// 中文处理
StringEntity se = new StringEntity(json, Consts.UTF_8);
httppost.setEntity(se);
HttpPost、HttpPut继承了HttpEntityEnclosingRequestBase类,所以有setEntity方法。详情请自行查看源码。
查看httpclient-4.2.3的源码可以发现,methods包下面包含HttpGet, HttpPost, HttpPut, HttpDelete等类来实现http的常用操作。
其中,HttpPost继承自 HttpEntityEnclosingRequestBase,HttpEntityEnclosingRequestBase类又实现了 HttpEntityEnclosingRequest接口,实现了setEntity的方法。
而HttpDelete继承自HttpRequestBase,没有实现setEntity的方法,因此无法设置HttpEntity对象。
而HttpDelete没有对应的setEntity()方法,那么怎么传递呢?
在网上找到了一种解决办法:
package net.dn.client;
import java.io.IOException;
import java.net.URI;
import org.apache.http.Header;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
@NotThreadSafe
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();
}
}
public class ClientMain {
public static void main(String[] args) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
String url = "http://localhost:8080/RESTfulDeleteWithBody/rest";
HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);
StringEntity input = new StringEntity("John Dow", ContentType.APPLICATION_JSON);
httpDelete.setEntity(input);
System.out.println("****REQUEST***************************************");
System.out.println(url);
Header requestHeaders[] = httpDelete.getAllHeaders();
for (Header h : requestHeaders) {
System.out.println(h.getName() + ": " + h.getValue());
}
CloseableHttpResponse response = httpclient.execute(httpDelete);
System.out.println("****RESPONSE***************************************");
System.out.println("----status:---------------------");
System.out.println(response.getStatusLine());
System.out.println("----header:---------------------");
Header responseHeaders[] = response.getAllHeaders();
for (Header h : responseHeaders) {
System.out.println(h.getName() + ": " + h.getValue());
}
System.out.println("----content:---------------------");
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
然后就可以直接调用了,调用方式如下;
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);