- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 elasticsearch Java API 并尝试进行 curl 调用以查明我的索引中是否存在文档。 This是如何在命令行中完成的。据我从这里的帖子可以看出,我应该使用 HttpURLConnection java 类或 apache httpclient 在 java 中发送 curl 请求。我的请求应该是这样的:
curl -i -XHEAD http://localhost:9200/indexName/mappingName/docID
。
关于如何通过 java 发送 curl 请求实际上有很多问题,但答案并没有那么解释 - 因此我不确定如何为 curl head 请求配置请求参数。到目前为止,我已经转载了this来自 Ashay 的回答,但它不起作用。
有没有人在 elasticsearch 的 java API 中发送 curl 调用并且可以解释如何做?
这是我的代码,我得到的错误是“java.net.MalformedURLException:无协议(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 请求进行编码。
最佳答案
以下片段可以作为起点。
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());
});
在默认的 elasticsearch 安装中使用 requestURL = "http://localhost:9200";
会返回
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 片段查询
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
关于java - 如何在 java 中制定 curl -XHEAD 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36982477/
我以前从未做过任何 curl ,所以需要一些帮助。我试图从示例中解决这个问题,但无法理解它! 我有一个 curl 命令,我可以从 Windows 命令行成功运行该命令,该命令行在 Solr 中索引 p
curl -v有什么区别和 curl -I ? 我可以看到 -v是冗长的和 -I是标题。有什么具体的吗? 最佳答案 -I (大写字母 i)在 curl 中表示“没有正文”,对于 HTTP 表示发送 H
我正在使用curl php API访问FTP链接。在特定站点上,它给出错误代码9(拒绝访问)。但是,可以从IE和Firefox访问该链接。 然后,我运行curl命令行,它给出了相同的“访问拒绝”结果。
我已经使用curl有一段时间了,它可以正常工作,但是使用使用用户'domain\username'来验证curl的代理时,无法请求授权。授权方法是NTLM。此代码放入批处理文件中。 代码: curl
“curl”默认使用哪些证书? 例子: curl -I -L https://cruises.webjet.com.au 在 Ubuntu 15.04 上失败 curl: (60) SSL certi
我知道终端输出的一部分是请求的持续时间,剩余时间等。但是是否有一些文档指定了curl命令的终端输出的每一列到底是什么?手册页上的内容非常稀疏。 最佳答案 可能不容易找到,但已在the curl boo
我想通过 curl 在我自己的云服务器上的特定文件夹中上传文件。例如:http://www.myowncloudserver.com/remote.php/webdav/{MY_FOLDER}。此时我
我的网站上有一个密码保护的Web文件夹,我正在使用Curl在另一个域上获取该文件夹,我想要的是:当我尝试打开URL时,应该问我用户名和密码,而不是让它显示“需要授权”。 例: http://www.e
有没有一种方法可以通过简单的Curl获取Rabbitmq中队列的大小(剩余消息)? 类似于curl -xget http://host:1234/api/queue/test/stats 谢谢 最佳答
关闭。这个问题是opinion-based .它目前不接受答案。 2年前关闭。 锁定。这个问题及其答案是locked因为这个问题是题外话,但具有历史意义。它目前不接受新的答案或互动。 我最近开始在我的
我想访问需要用户名/密码的 URL。我想尝试用curl 访问它。现在我正在做类似的事情: curl http://api.somesite.com/test/blah?something=123 我收
我正在尝试使用 CURL 进行查询ElasticSearch 中的命令在windows平台。 例如:localhost:9200/playground/equipment/1?pretty 我收到一条
我正在尝试使用 Docker 构建和运行 Marklogic 实例。 Marklogic 提供了一些不错的 http api,所以,作为最终 CMD在 Dockerfile 中,我运行两个脚本,它们通
我正在尝试通过 cURL 检索网页的内容(比方说 http://www.foo.com/bar.php )。 当我在浏览器中加载网站时,加载页面时会出现动画,页面最终会显示出来。 但是使用 cURL,
我正在尝试使用带代理的命令行 CURL 获取响应状态代码。 这会返回整个页面,但我只想要状态代码。我怎么做?谢谢。 curl -sL -w -x IP:PORT "%{http_code}\n""ht
我有一段代码检查 http/s 端点的状态和加载时间。然后我会为每个顶级页面检查 1 级 href,以检查页面引用的所有内容是否也加载了 200。 (我查了50个顶级页面,每个顶级页面平均有8个链接)
curl --upload-file 和 curl --form file=@/path/file 有什么区别?这些 HTTP 请求有何不同? 最佳答案 --上传文件 (使用 HTTP 或 HTTPS
我正在尝试使用 system-curl 安装 cmake,使用 ./bootstrap --system-curl,如 here 所示.这样做,我得到了: -- Could NOT find
我需要使用 Curl 下载 Youtube 视频的特定部分。 (假设我想下载前 2MB)我在 Curl 中使用 -r 开关来实现这一点。它适用于非 YouTube 链接,但 Youtube 链接会忽略
我希望在使用 curl 命令从远程服务器下载文件后,将时间戳或日期添加到文件名中。我知道您可以使用 -o 来指定您要为文件命名的内容。我看到过这样的建议:-o "somefile $(date +\"
我是一名优秀的程序员,十分优秀!