fix pasting to hastebin

This commit is contained in:
Kermina Awad 2020-06-06 20:05:33 -04:00
parent d69c492577
commit e821611744

View File

@ -9,6 +9,7 @@ import java.io.OutputStreamWriter;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
/** /**
@ -43,14 +44,19 @@ public class HastebinPasteService implements PasteService {
URLConnection conn = url.openConnection(); URLConnection conn = url.openConnection();
conn.setDoOutput(true); conn.setDoOutput(true);
wr = new OutputStreamWriter(conn.getOutputStream()); // hastebin needs a user-agent
rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); conn.addRequestProperty("User-Agent", "placeholder");
// this isn't required, but is technically correct
conn.addRequestProperty("Content-Type", "text/plain; charset=utf-8");
wr = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8.newEncoder());
wr.write(encodedData); wr.write(encodedData);
wr.flush(); wr.flush();
String line; String line;
StringBuilder responseString = new StringBuilder(); StringBuilder responseString = new StringBuilder();
// this has to be initialized AFTER the data has been flushed!
rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
while ((line = rd.readLine()) != null) { while ((line = rd.readLine()) != null) {
responseString.append(line); responseString.append(line);
} }