如何使用java应用程序请求弹性搜索URL?

时间:2014-04-30 08:53:44

标签: java http elasticsearch

当我浏览弹性搜索的网址时,当我尝试通过代码连接时,它会返回400错误的请求错误。当我请求www.google.com时,它会返回'200 OK'。可能是什么问题?

这是我的代码:

      url = new URL("http://192.168.1.68:9200");
      httpCon = (HttpURLConnection) url.openConnection();
      httpCon.setDoOutput(true);
      httpCon.setRequestMethod("POST");
      out = new OutputStreamWriter(
      httpCon.getOutputStream());
      int responseCode = httpCon.getResponseCode();
      String responseMessage = httpCon.getResponseMessage();
      System.out.println(responseCode);
      System.out.println(responseMessage);
      if (httpCon.getResponseCode()==HttpURLConnection.HTTP_OK){
          JOptionPane.showMessageDialog(rootPane, "Connection Successful");
      }
      else {
          JOptionPane.showMessageDialog(rootPane, "Connection Error!");
      }
      out.Close();

1 个答案:

答案 0 :(得分:0)

您没有传递任何搜索参数...例如:

url = new URL("http://192.168.1.68:9200");

应该是

url = new URL("http://192.168.1.68:9200/_search?q=whatever+you+want");

或者你应该通过写入流出然后关闭它来发布你的JSON格式的查询(但是你仍然需要在你的URL上标记/ _search。)