我正在使用Apache Http客户端来使用ElasticSearch Rest Api,但我总是得到HTTP错误代码为200.请帮忙
Java代码
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.Scanner;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public class ApacheHttpClientPost {
public static void main(String[] args) {
String path="C:\\Tools\\ElasticSearchApi\\javadoc.txt", filecontent="";
ApacheHttpClientPost apacheHttpClientPost = new ApacheHttpClientPost();
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://localhost:9200/versioneg/message/_percolate");
filecontent=apacheHttpClientPost.readFileContent(path);
System.out.println(filecontent);
StringEntity input = new StringEntity(filecontent);
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
if (response.getStatusLine().getStatusCode() != 201) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
httpClient.getConnectionManager().shutdown();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String readFileContent(String pathname) throws IOException {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int)file.length());
Scanner scanner = new Scanner(file);
String lineSeparator = System.getProperty("line.separator");
try {
while(scanner.hasNextLine()) {
fileContents.append(scanner.nextLine() + lineSeparator);
}
return fileContents.toString();
} finally {
scanner.close();
}
}
}
控制台
{
"doc": {
"os": "Linux",
"product": {
"name": "abc",
"version": 10.1,
"configversion": 1,
"arch": 32,
"license": "commercial",
"db": {
"@type": "Oracle"
}
}
}
}
Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 200
at com.informatica.zanshin.esapi.utils.ApacheHttpClientPost.main(ApacheHttpClientPost.java:31)
这是elasticsearch sense screenshot
答案 0 :(得分:7)
状态代码200代表“确定”
check w3c ref
你应该使用
if(response.getStatusLine().getStatusCode() != 200){
// Throw exception or something else
}
答案 1 :(得分:0)
稍加修改后,您的代码就会运行。由于 DefaultHttpClient 现在已弃用,您需要使用 HttpClient 并且无需更改状态代码,因为我在post请求中验证它返回响应代码201并在获取请求它返回200.如果你知道提琴手,我还附上了fiddler的会话截图。如果您不了解提琴手,可以访问http://www.telerik.com/fiddler
try {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost("http://localhost:9200/versioneg/message/");
StringEntity requestEntity = new StringEntity(resultJsonObject.toString(), ContentType.APPLICATION_JSON);
System.out.println("resultJsonobject: "+ resultJsonObject.toString());
postRequest.setEntity(requestEntity);
HttpResponse response = httpClient.execute(postRequest);
if (response.getStatusLine().getStatusCode() != 201) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (Exception e) {
e.printStackTrace();
}