我需要调用数据库api https://hosturl/query?apiToken=authenticated_API_Token 并需要传递请求有效负载:-
{ "\r\n" +
"\"filterString\": \"([62wUXYCDt0WnOnO18psHbQ.ZPyw3IUxsUWvywjWCEuAdQ.QEfefkYB5EqgogjWCEuoBg] ge Today() and Date([62wUXYCDt0WnOnO18psHbQ.ZPyw3IUxsUWvywjWCEuAdQ.KR-iuFdqYUCnjgjW8zNMoQ]) le Today())\",\r\n"
"\r\n" +
"}
它将通过电话回复。
我用JS尝试过:-
xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", "https://hosturl/query?apiToken=someToken", true);
xmlHttp.setRequestHeader("Content-Type", "application/json");
xmlHttp.send("{ "\r\n" +
"\"filterString\": \"([62wUXYCDt0WnOnO18psHbQ.ZPyw3IUxsUWvywjWCEuAdQ.QEfefkYB5EqgogjWCEuoBg] ge Today() and Date([62wUXYCDt0WnOnO18psHbQ.ZPyw3IUxsUWvywjWCEuAdQ.KR-iuFdqYUCnjgjW8zNMoQ]) le Today())\",\r\n"
"\r\n" +
"}");
我在这里得到适当的答复。
但是当我尝试使用Java来获取连接超时时。
final URL url = new URL(https://hosturl/query?apiToken=someToken);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
final String payLoad ="{ "\r\n" +
"\"filterString\": \"([62wUXYCDt0WnOnO18psHbQ.ZPyw3IUxsUWvywjWCEuAdQ.QEfefkYB5EqgogjWCEuoBg] ge Today() and Date([62wUXYCDt0WnOnO18psHbQ.ZPyw3IUxsUWvywjWCEuAdQ.KR-iuFdqYUCnjgjW8zNMoQ]) le Today())\",\r\n"
"\r\n" +
"}";
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type","application/json") ;
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Length", Integer.toString(payLoad.length()));
conn.setUseCaches(false);
final java.io.OutputStream os = conn.getOutputStream();
try (java.io.OutputStreamWriter wr = new java.io.OutputStreamWriter(os);) {
wr.write(data);
wr.flush();
}
final int responseCode = conn.getResponseCode();
if (responseCode == 200 || responseCode == 201) {
final StringBuilder response = new StringBuilder();
try (java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
}
responseString = response.toString();
} else {
}
conn.disconnect();
} catch (final Exception ex) {
LOG.error("Exception ", ex);
} finally {
if (conn != null) {
conn.disconnect();
}
}
可能是什么原因?