Last active
December 7, 2021 04:17
-
-
Save VitorBlog/b142abb5601ca1fe43edce5a134e0c40 to your computer and use it in GitHub Desktop.
Create a hastebin using Java
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
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