Skip to content

Instantly share code, notes, and snippets.

@VitorBlog
Last active December 7, 2021 04:17
Show Gist options
  • Save VitorBlog/b142abb5601ca1fe43edce5a134e0c40 to your computer and use it in GitHub Desktop.
Save VitorBlog/b142abb5601ca1fe43edce5a134e0c40 to your computer and use it in GitHub Desktop.
Create a hastebin using Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.stream.Collectors;
public class HasteBin {
public static String pasteText(String text) throws IOException {
URL url = new URL("https://hastebin.com/documents");
URLConnection connection = url.openConnection();
connection.setRequestProperty("authority", "hastebin.com");
connection.setRequestProperty("accept", "application/json, text/javascript, /; q=0.01");
connection.setRequestProperty("x-requested-with", "XMLHttpRequest");
connection.setRequestProperty("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36'");
connection.setRequestProperty("content-type", "application/json; charset=UTF-8");
connection.setDoOutput(true);
OutputStream stream = connection.getOutputStream();
stream.write(text.getBytes());
stream.flush();
stream.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = reader.lines().collect(Collectors.joining("\n"));
return "https://hastebin.com/" + response.split("\"")[3];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment