public Answer commonRequest(String path, JsonObject params) throws IOException {
synchronized (this) {
String charset = "UTF-8";
URLConnection connection = new URL(url + "/" + path).openConnection();
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.setReadTimeout(CONNECTION_READ_TIMEOUT);
connection.setRequestProperty("User-Agent", "Universa JAVA API Client");
if(!params.isEmpty()) {
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
try (
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
) {
String json = params.toString();
writer.append(json);
}
}
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
byte[] answer = Do.read(httpConnection.getInputStream());
httpConnection.disconnect();
JsonValue json = Json.parse(new String(answer));
return new Answer(responseCode, Binder.of("response",ofJson(json)));
}
}