C# 发出HTTP POST请求时捕获ElasticSearch异常
我使用ElasticSearchC# 发出HTTP POST请求时捕获ElasticSearch异常,c#,visual-studio,elasticsearch,C#,Visual Studio,elasticsearch,我使用ElasticSearch7.2.0。我使用System.Net.Http.HttpClient类向我的ElasticSearch服务器发出POST请求: private const string Host = "http://127.0.0.1"; private const int Port = 9200; public static async Task<HttpResponseMessage> Receive(string jsonPostContents) {
7.2.0
。我使用System.Net.Http.HttpClient
类向我的ElasticSearch服务器发出POST
请求:
private const string Host = "http://127.0.0.1";
private const int Port = 9200;
public static async Task<HttpResponseMessage> Receive(string jsonPostContents)
{
try
{
return await this.httpClient.PostAsync($"{Host}:{Port}/_bulk?pipeline=ParseDuration",
new StringContent(jsonPostContents, Encoding.UTF8, mediaType: MimeTypes.Json));
}
catch (Exception exception)
{
StringContent responseString = new StringContent($"Exception encountered: {exception.Message}.\n" +
$"Inner exception: {exception.InnerException?.Message}\n" +
$"Stack trace: {exception.StackTrace}");
return new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = responseString };
}
}
但是,Receive(string jsonPostContents)
方法不是返回HTTP错误响应,而是返回OK
响应(使用HTTP状态代码200
)
如何确保可靠捕获所有ElasticSearch异常?批量API仅在存在连接问题时返回错误,否则返回200 您应该检查来自批量的响应。如果批量中只有一个错误,则响应如下所示:
{
"took": x,
"errors": true,
"items":[
//element i: shows what happened with the item i from bulk
]
}
批量API仅在存在连接问题时返回错误,否则返回200 您应该检查来自批量的响应。如果批量中只有一个错误,则响应如下所示:
{
"took": x,
"errors": true,
"items":[
//element i: shows what happened with the item i from bulk
]
}
你有没有想过用它?您可以免费获得相当可靠的错误处理。您想过使用吗?您可以免费获得相当可靠的错误处理。