我希望在java代码中使用以下curl请求。我看到我们可以使用httpget来调用休息服务。
这是我的curl命令:
<xsd:element name="b" type="CT_OnOff" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Bold</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:complexType name="CT_OnOff">
<xsd:attribute name="val" type="ST_OnOff">
<xsd:annotation>
<xsd:documentation>On/Off Value</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:simpleType name="ST_OnOff">
<xsd:annotation>
<xsd:documentation>On/Off Value</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="true">
<xsd:annotation>
<xsd:documentation>True</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="false">
<xsd:annotation>
<xsd:documentation>False</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="on">
<xsd:annotation>
<xsd:documentation>True</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="off">
<xsd:annotation>
<xsd:documentation>False</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="0">
<xsd:annotation>
<xsd:documentation>False</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="1">
<xsd:annotation>
<xsd:documentation>True</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
</xsd:restriction>
</xsd:simpleType>
如何将该命令放入我的HttpGet curl -XGET 'localhost:9200/indexname/status/_search' -d '{"_source": {"include": [ "field1", "name1" ]}, "query" : {"term": { "Date" :"2000-12-23T10:12:05" }}}'
请指教。感谢。
答案 0 :(得分:0)
组合-X GET and -d会导致您的数据以application / x-www-form-urlencoded格式附加到网址。
因此,我建议使用URLEncoder,如下所示:
String host = "localhost:9200/indexname/status/_search";
String data = "{\"_source\": {\"include\": [ \"field1\", \"name1\" ]}, \"query\" : {\"term\": { \"Date\" :\"2000-12-23T10:12:05\" }}}";
String url = host + "?" + URLEncoder.encode(data, "UTF-8");
答案 1 :(得分:0)
您可以使用HttpURLConnection。 此代码是我认为适合您的示例:
public void get() throws IOException{
//Create a URL object.
String url = "localhost:9200/indexname/status/_search";
URL getURL = new URL(url);
//Establish a https connection with that URL.
HttpsURLConnection con = (HttpsURLConnection) getURL.openConnection();
//Select the request method, in this case GET.
con.setRequestMethod("GET");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
String parameters = "{\"_source\": {\"include\": [ \"field1\", \"name1\" ]}, \"query\" : {\"term\": { \"Date\" :\"2000-12-23T10:12:05\" }}}";
//Write the parameter into the Output Stream, flush the data and then close the stream.
wr.writeBytes(parameters);
wr.flush();
wr.close();
System.out.println("\nSending 'GET' request to URL : " + url);
int responseCode;
try {
responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
} catch (Exception e) {
System.out.println("Error: Connection problem.");
}
//Read the POST response.
InputStreamReader isr = new InputStreamReader(con.getInputStream());
BufferedReader br = new BufferedReader(isr);
StringBuffer response = new StringBuffer();
String inputLine;
while ((inputLine = br.readLine()) != null) {
//Save a line of the response.
response.append(inputLine + '\n');
}
br.close();
System.out.println(response.toString());
}
如果这不起作用,因为我必须对参数进行错误输入,无论如何都要尝试