【问题标题】:How to formulate curl -XHEAD request in java如何在java中制定curl -XHEAD请求
【发布时间】:2016-08-27 05:17:21
【问题描述】:

我正在使用 elasticsearch Java API 并尝试进行 curl 调用以查明文档是否存在于我的索引中。 This 是在命令行中完成的。据我从这里的帖子中可以看出,我应该使用 HttpURLConnection java 类或 apache httpclient 在 java 中发送 curl 请求。 我的要求应该是这样的:

curl -i -XHEAD http://localhost:9200/indexName/mappingName/docID

实际上有很多关于如何通过 java 发送 curl 请求的问题,但答案不是那么解释 - 因此我不确定如何配置 curl head 请求的请求参数。到目前为止,我已经复制了来自 Ashay 的 this 答案,但它不起作用。

有没有人在 elasticsearch 的 java API 中发送 curl 调用并能解释如何做?

这是我的代码,我得到的错误是“java.net.MalformedURLException: no protocol”

import org.apache.commons.codec.binary.Base64;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

String encodedURL = URLEncoder.encode(new StringBuilder()
    .append("http://").append(serverName).append(":9200/") // elasticsearch port
    .append(indexName).append("/").append(mappingName).append("/")
    .append(url).toString(), "UTF-8"); // docID is a url
System.out.print("encodedURL : " + encodedURL + "\n");

URL url = new URL(new StringBuilder().append(encodedURL).toString());
System.out.print("url "+ url.toString() + "\n");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("X-Requested-With", "Curl");
connection.setRequestMethod("HEAD");

String userpass = new StringBuilder().append(username).append(":").append(password).toString();
String basicAuth = new StringBuilder().append("Basic ").append(new String(new Base64().encode(userpass.getBytes()))).toString();
connection.setRequestProperty("Authorization", basicAuth);
String inputLine;
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

附:该索引的文档 id 是 url,这就是我需要对它们进行编码的原因。另一方面,我不确定是否应该对完整的 http 请求进行编码。

【问题讨论】:

    标签: java http url curl elasticsearch


    【解决方案1】:

    跟随 sn-p 可能是一个起点。

    String serverName = "localhost";
    String indexName = "index_name";
    String mappingName = "mapping_name";
    String docId = "FooBarId";
    
    String username = "JohnDoe";
    String password = "secret";
    
    String requestURL = String.format("http://%s:9200/%s/%s/%s",
            serverName,
            indexName,
            mappingName,
            docId
    );
    System.out.println("requestURL: " + requestURL);
    
    URL url = new URL(requestURL);
    System.out.println("URL: " + url);
    
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("X-Requested-With", "Curl");
    connection.setRequestMethod("HEAD");
    
    String credentials = username + ":" + password;
    
    Base64.Encoder encoder = Base64.getEncoder();
    String basicAuth = "Basic " + encoder.encodeToString(credentials.getBytes());
    
    connection.setRequestProperty("Authorization", basicAuth);
    
    connection.getHeaderFields()
            .entrySet()
            .forEach((Entry<String, List<String>> t) -> {
                System.out.printf("%-20s : %s%n", t.getKey(), t.getValue());
            });
    

    requestURL = "http://localhost:9200"; 与默认的elasticsearch 安装一起使用会返回

    requestURL: http://localhost:9200
    URL: http://localhost:9200
    null                 : [HTTP/1.1 200 OK]
    Content-Length       : [0]
    Content-Type         : [text/plain; charset=UTF-8]
    

    添加也许您可以尝试类似于以下步骤的操作。根据您的需要修改它们。也许你可以跳过第一步。

    索引一些东西

    curl -XPUT "http://localhost:9200/books/book/1" -d'
    {
        "title": "The Hitchhikers Guide to the Galaxy",
        "author": "Douglas Adams",
        "year": 1978
    }'
    

    从命令行查询

    curl -X GET http://localhost:9200/books/book/1
    

    输出

    {"_index":"books","_type":"book","_id":"1","_version":1,"found":true,"_source":
    {
        "title": "The Hitchhikers Guide to the Galaxy",
        "author": "Douglas Adams",
        "year": 1978
    }}
    

    使用上述 Java sn-p 查询

    String serverName = "localhost";
    String indexName = "books";
    String mappingName = "book";
    String docId = "1";
    String requestURL = String.format("http://%s:9200/%s/%s/%s",
            serverName,
            indexName,
            mappingName,
            docId
    );
    System.out.println("requestURL: " + requestURL);
    URL url = new URL(requestURL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.getHeaderFields()
            .entrySet()
            .forEach((Entry<String, List<String>> t) -> {
                System.out.printf("%-20s : %s%n", t.getKey(), t.getValue());
            });
    try (InputStream inputStream = connection.getInputStream()) {
        for (int i = inputStream.read(); i > -1; i = inputStream.read()) {
            System.out.print((char) i);
        }
    }
    

    输出

    requestURL: http://localhost:9200/books/book/1
    null                 : [HTTP/1.1 200 OK]
    Content-Length       : [184]
    Content-Type         : [application/json; charset=UTF-8]
    {"_index":"books","_type":"book","_id":"1","_version":1,"found":true,"_source":
    {
        "title": "The Hitchhikers Guide to the Galaxy",
        "author": "Douglas Adams",
        "year": 1978
    }}
    

    该示例使用默认的 elasticsearch 安装。

    取决于您真正想要实现的目标。你最好使用elasticsearch TransportClient

    import java.net.InetAddress;
    import org.elasticsearch.action.get.GetRequestBuilder;
    import org.elasticsearch.action.get.GetResponse;
    import org.elasticsearch.client.Client;
    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.transport.InetSocketTransportAddress;
    
    public class GetDemo {
    
        public static void main(String[] args) throws Exception {
            InetAddress hostAddr = InetAddress.getByName("localhost");
            InetSocketTransportAddress socketAddr =
                    new InetSocketTransportAddress(hostAddr, 9300);
            try (Client client = TransportClient.builder().build()
                    .addTransportAddress(socketAddr)) {
                GetRequestBuilder request = client.prepareGet("books", "book", "1");
                GetResponse response = request.execute().actionGet();
                response.getSource()
                        .forEach((k, v) -> System.out.printf("%-6s: %s%n", k, v));
            }
        }
    }
    

    输出

    ...
    year  : 1978
    author: Douglas Adams
    title : The Hitchhikers Guide to the Galaxy
    

    【讨论】:

    • 感谢您的回复。您的示例代码为所有 docId 生成“null=[HTTP/1.1 400 Bad Request] Content-Length=[145] Content-Type=[text/plain; charset=UTF-8]”错误。我所做的唯一更改是将 localhost 替换为我们的服务器名称,并使用“Base64.encodeBase64(credentials.getBytes());”因为 Base64.Encoder 是私有的
    • @KonstantinaLazaridou Base64.Encoder 是 Java 8 中的 public static class。你检查过你生成的 urlbasicAuth 吗?因为HTTP 400The request could not be understood by the server due to malformed syntax.
    • 是的,网址看起来不错,用户信息也正确。服务器是否可能使用与 Base64 不同的加密系统?我还赞扬了授权属性(如果没有必要),但仍然是同样的错误。我也不知道.setRequestProperty("X-Requested-With", "Curl");在语法上是否正确。
    • @尝试解决问题。 1) 在浏览器中输入您要访问的网址。 a) 网址是否有效。 b) 您是否收到用户/密码的请求弹出窗口。 2) 如果 url 正常工作,则在上面的示例中将其作为固定字符串分配给 requestURL。另一个问题是您的 curl 示例在没有用户/密码的情况下工作?如果是这样的话。为什么你认为你在 java 代码中需要它?
    猜你喜欢
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多