Skip to content

Instantly share code, notes, and snippets.

@glourenco
Last active November 27, 2018 15:58
Show Gist options
  • Save glourenco/634391015e9e5d1e83bf661f06d2b63d to your computer and use it in GitHub Desktop.
Save glourenco/634391015e9e5d1e83bf661f06d2b63d to your computer and use it in GitHub Desktop.
private String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public JSONObject readJsonFromUrl(String url) {
try {
URLConnection connection = new URL(url).openConnection();
HttpsURLConnection httpConn = (HttpsURLConnection) connection;
httpConn.setRequestProperty("Authorization", "Bearer " + rancherBearerToken);
InputStream is = httpConn.getInputStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} catch (JSONException ex) {
Logger.getLogger(RancherClient.class.getName()).log(Level.SEVERE, null, ex);
} finally {
is.close();
}
} catch (IOException ex) {
Logger.getLogger(RancherClient.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment