es应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.8 KiB

package com.bfd.operate.entity;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
/**
* @author:jinming
* @className:HttpDeleteWithBody
* @version:1.0
* @description:
* @Date:2024/6/26 18:47
*/
public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "DELETE";
/**
* 获取方法(必须重载)
*
* @return
*/
@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();
}
public static void main(String[] args) throws Exception {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "baifendian"));
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultCredentialsProvider(credentialsProvider)
.build();
String url = "http://172.18.1.146:9200/_search/scroll";// 地址
Map<String, String> params = new HashMap<>();// 参数
params.put("scroll_id", "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFm82STlTbHFhVHpPdlhCY1BXZzhDdVEAAAAAAFByLBZuRmtpNUN3UVJoU3c2Q2M1Y1pVazBn");
// 创建默认的httpClient实例.
//以delte方式请求
HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);
// 设置请求头
httpDelete.setHeader("Content-Type", "application/json;charset=UTF-8");
httpDelete.setHeader("accept", "application/json");
//将参数以UTF-8编码并包装成表单实体对象
StringEntity se = new StringEntity(new Gson().toJson(params), "UTF-8");
se.setContentType("text/json");
httpDelete.setEntity(se);
// 执行请求
CloseableHttpResponse response = httpClient.execute(httpDelete);
// 获取返回值
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
System.out.println("打印结果:" + result);
}
}