1. 自定义一个类,实现HttpEntityEnclosingRequestBase
package nc.plugins.http;
import java.net.URI;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
/**
* 自定义带body的httpdelete请求
* @author phelix
*
*/
public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase{
public static final String METHOD_NAME = "DELETE";
@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();
}
}
2. 使用上面的自定义类处理Http Delete请求
public String doDelete(String url, String param) {
BufferedReader br = null;
JSONObject result = new JSONObject();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);
httpDelete.setHeader("Content-Type", "application/json;charset=utf-8");
try {
HttpEntity entity = new StringEntity(param);
httpDelete.setEntity(entity);
HttpResponse response = httpClient.execute(httpDelete);
InputStream is = response.getEntity().getContent();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
return sbf.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
参考: http://www.docjar.com/html/api/org/apache/http/client/methods/HttpPost.java.html