Я хочу вызвать http url и получить некоторую json полезную нагрузку, используя Httpsurlconnection
с методом post и телом отправки json и некоторым параметром заголовка. Ниже приведен мой код, и я получаю сообщение об ошибке: недостаточно данных записано
JAVA
public Map<String, Object> makeHttpPostRequestCall(String httpURL, Map<String, Object> apiParamsMap) {
logger.info("call request call");
String nonce = UUID.randomUUID().toString();
String encodedNonce = Base64.getEncoder().encodeToString(nonce.getBytes());
String token = UtilService.getString(apiParamsMap, RyderConnectConstants.PRIMARY_KEY);
JSONObject body = new JSONObject();
body.put("startDateTime", "2017-07-21T17:32:28Z");
body.put("endDateTime", "2017-07-22T17:32:28Z");
BufferedReader reader = null;
URL url;
Map<String, Object> feedPaylaod = null;
try {
url = new URL(httpURL);
URLConnection connection = url.openConnection();
HttpsURLConnection httpConn = (HttpsURLConnection) connection;
httpConn.setRequestProperty("Content-Type", "application/json");
httpConn.setRequestProperty("nonce", encodedNonce);
httpConn.setRequestProperty("Ocp-Apim-Subscription-Key", token);
httpConn.setDoOutput(true);
httpConn.setRequestProperty("Body", body.toString());
logger.info("pp1");
httpConn.setRequestProperty( "charset", "utf-8");
httpConn.setFixedLengthStreamingMode(body.toString().length());
httpConn.setRequestMethod("POST");
logger.info("pp");
DataOutputStream out = new DataOutputStream(httpConn.getOutputStream());
out.write(body.toString());
out.close();
reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
logger.info("reader"+reader);
String line = null;
StringWriter out = new StringWriter(connection.getContentLength() > 0 ? connection.getContentLength() : 20480);
logger.info("out"+out);
while ((line = reader.readLine()) != null) {
out.append(line);
}
String response = out.toString();
feedPaylaod = stringToMap(response);
} catch (Exception e) {
feedPaylaod = Maps.newHashMap();
logger.error(e.getMessage());
}
return feedPaylaod;
}