HttpPost请求Elasticsearch

package com.train.monitoring.Reptile_mobile_monitoring.util;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class ESConfig {
    
      public String get(){
          DefaultHttpClient httpClient = new DefaultHttpClient();
          String url="http://101.236.57.141:5601/api/saved_objects/_find?type=index-pattern&per_page=10000";
            HttpGet httpGet = new HttpGet(url);
     
            httpGet.addHeader("Content-Type", "application/json;charset=utf-8");

            // 执行请求
            HttpResponse response=null;
            JSONObject jsonObject=null;
            try {
                response = httpClient.execute(httpGet);
                String json2 = EntityUtils.toString(response.getEntity(), "utf-8");
                jsonObject = JSONObject.parseObject(json2);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
     
            // 打印执行结果
            System.out.println(jsonObject);
          
        return null;
      }
      
      
      public static JSONObject post(String url){
          DefaultHttpClient httpClient = new DefaultHttpClient();
          /**
           * curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
'
           */
//            String url = "http://101.236.47.119:9200/redis-log-2018.07.26/doc/_search?pretty";
            HttpPost httpPost = new HttpPost(url);
     
            // 设置请求的header
            httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
//            String param="{"+
//                    "\"query\": { \"match_all\": {} },"+
//                    "\"from\": 10,"+
//                    "\"size\": 10"+
//                    "}";
            String param="{"+
                    "\"query\": { \"match_phrase\": {\"biztype\":\"排队的 代理IP为:\"} },"+
                    "\"from\": 1,"+
                    "\"size\": 9000"+
                    "}";
//            String param="{"+
//                    "  \"query\": {"+
//                    "    \"bool\": {"+
//                    "      \"should\": ["+
//                    "        { \"match\": { \"biztype\": \"排队的代理IP为:\" } }"+
//                //    "        { \"match\": { \"address\": \"lane\" } }"+
//                    "      ]"+
//                    "    }"+
//                    "  }"+
//                    "}";
            // 设置请求的参数
            JSONObject jsonParam = JSONObject.parseObject(param);
           
            
            StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            JSONObject jsonObject=null;
            try {
                
                // 执行请求
                HttpResponse response = httpClient.execute(httpPost);
                String json2 = EntityUtils.toString(response.getEntity(), "utf-8");
                jsonObject = JSONObject.parseObject(json2);
            } catch (Exception e) {
                e.printStackTrace();
            }
     
            // 打印执行结果
            return jsonObject;

      }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值