Skip to content

Instantly share code, notes, and snippets.

@FrostDelta123
Created September 20, 2018 14:42
Show Gist options
  • Save FrostDelta123/6ac899dcd224249687adb183d3501967 to your computer and use it in GitHub Desktop.
Save FrostDelta123/6ac899dcd224249687adb183d3501967 to your computer and use it in GitHub Desktop.
public static String paste(String urlParameters) {
HttpURLConnection connection = null;
try {
URL url = new URL(pasteURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
wr.write(urlParameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) response.append(inputLine);
in.close();
JsonElement json = new JsonParser().parse(response.toString());
if (!json.isJsonObject()) throw new IOException("error");
return json.getAsJsonObject().get("key").getAsString();
} catch (IOException e) {
return null;
} finally {
if (connection == null) return null;
connection.disconnect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment