DeleteUtil
import java.util.Map;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
/**
* DELETE请求工具类
*/
public class DeleteUtil {
public static final String UTF8 = "utf-8";
public static String send(String url, Map<String, String> headers) throws IOException {
return doAction(url, headers);
}
private static String doAction(String url, Map<String, String> headers) throws IOException {
HttpDelete delete = new HttpDelete(url);
for (String key : headers.keySet()) {
delete.addHeader(key, headers.get(key));
}
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(delete);
HttpEntity entity = response.getEntity();
String result = null;
if (entity != null) {
result = EntityUtils.toString(entity, UTF8);
}
response.close();
return result;
}
}
DeleteUtil是一个Java类,使用ApacheHTTP客户端库执行DELETE类型的HTTP请求。它接受URL和自定义头部信息,发送请求并返回响应的实体内容。核心方法是doAction,它配置请求头,执行请求,并处理响应。

5447

被折叠的 条评论
为什么被折叠?



