Last active
November 27, 2018 15:58
-
-
Save glourenco/634391015e9e5d1e83bf661f06d2b63d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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