in documentation-samples/quickstarts/change-model/java/AddUtterances.java [89:122]
private LuisResponse call(String endpoint, String method, byte[] data) throws Exception {
// initialize HTTP connection
URL url = new URL(this.host + this.path + endpoint);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod(method);
conn.setRequestProperty("Ocp-Apim-Subscription-Key", key);
// handle POST request
if (method.equals(POST)) {
if (data == null)
data = new byte[]{}; // make zero-length body for POST w/o data
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Length", Integer.toString(data.length));
try (OutputStream ostream = conn.getOutputStream()) {
ostream.write(data, 0, data.length);
}
}
// Get response from API call. If response is an HTTP error, the JSON
// response is on the error string. Otherwise, it's on the input string.
InputStream stream;
try {
stream = conn.getInputStream();
} catch (IOException ex) {
stream = conn.getErrorStream();
}
String body = new Scanner(stream, UTF8).useDelimiter("\\A").next();
return new LuisResponse(body, conn.getResponseCode(), conn.getResponseMessage());
}