Java Delete请求工具类DeleteUtil

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;
    }
}

你可能感兴趣的:(编程技术,java,restful,delete)