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

7 months ago
  1. package com.bfd.operate.entity;
  2. import com.google.gson.Gson;
  3. import org.apache.http.HttpEntity;
  4. import org.apache.http.auth.AuthScope;
  5. import org.apache.http.auth.UsernamePasswordCredentials;
  6. import org.apache.http.client.CredentialsProvider;
  7. import org.apache.http.client.methods.CloseableHttpResponse;
  8. import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
  9. import org.apache.http.entity.StringEntity;
  10. import org.apache.http.impl.client.BasicCredentialsProvider;
  11. import org.apache.http.impl.client.CloseableHttpClient;
  12. import org.apache.http.impl.client.HttpClients;
  13. import org.apache.http.util.EntityUtils;
  14. import java.net.URI;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. /**
  18. * @author:jinming
  19. * @className:HttpDeleteWithBody
  20. * @version:1.0
  21. * @description:
  22. * @Date:2024/6/26 18:47
  23. */
  24. public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
  25. public static final String METHOD_NAME = "DELETE";
  26. /**
  27. * 获取方法必须重载
  28. *
  29. * @return
  30. */
  31. @Override
  32. public String getMethod() {
  33. return METHOD_NAME;
  34. }
  35. public HttpDeleteWithBody(final String uri) {
  36. super();
  37. setURI(URI.create(uri));
  38. }
  39. public HttpDeleteWithBody(final URI uri) {
  40. super();
  41. setURI(uri);
  42. }
  43. public HttpDeleteWithBody() {
  44. super();
  45. }
  46. public static void main(String[] args) throws Exception {
  47. CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  48. credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "baifendian"));
  49. CloseableHttpClient httpClient = HttpClients.custom()
  50. .setDefaultCredentialsProvider(credentialsProvider)
  51. .build();
  52. String url = "http://172.18.1.146:9200/_search/scroll";// 地址
  53. Map<String, String> params = new HashMap<>();// 参数
  54. params.put("scroll_id", "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFm82STlTbHFhVHpPdlhCY1BXZzhDdVEAAAAAAFByLBZuRmtpNUN3UVJoU3c2Q2M1Y1pVazBn");
  55. // 创建默认的httpClient实例.
  56. //以delte方式请求
  57. HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);
  58. // 设置请求头
  59. httpDelete.setHeader("Content-Type", "application/json;charset=UTF-8");
  60. httpDelete.setHeader("accept", "application/json");
  61. //将参数以UTF-8编码并包装成表单实体对象
  62. StringEntity se = new StringEntity(new Gson().toJson(params), "UTF-8");
  63. se.setContentType("text/json");
  64. httpDelete.setEntity(se);
  65. // 执行请求
  66. CloseableHttpResponse response = httpClient.execute(httpDelete);
  67. // 获取返回值
  68. HttpEntity entity = response.getEntity();
  69. String result = EntityUtils.toString(entity, "UTF-8");
  70. System.out.println("打印结果:" + result);
  71. }
  72. }