aNNiMON

PastebinSender

Jun 28th, 2012
128
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.pastebin.pastebinplugin;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8.  
  9. /**
  10.  * Send code to pastebin.com.
  11.  * @author aNNiMON
  12.  */
  13. public class PastebinSender {
  14.    
  15.     private static final String URL = "http://pastebin.com/api/api_post.php";
  16.    
  17.     /**
  18.      * Send request to pastebin.
  19.      * @param request @see http://pastebin.com/api
  20.      * @return pasted url or error message.
  21.      */
  22.     public static String sendRequest(String request) {
  23.         try {
  24.             URL url = new URL(URL);
  25.             HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
  26.             httpConn.setRequestMethod("POST");
  27.             httpConn.setInstanceFollowRedirects(false);
  28.             httpConn.setDoOutput(true);
  29.             httpConn.connect();
  30.            
  31.             OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
  32.             out.write(request);
  33.             out.flush();
  34.            
  35.             StringBuilder response = new StringBuilder();
  36.             BufferedReader in = new BufferedReader( new InputStreamReader(httpConn.getInputStream()) );
  37.             String line;
  38.             while ((line = in.readLine()) != null) {
  39.                 response.append(line).append('\n');
  40.             }
  41.             in.close();
  42.             return response.toString();
  43.         } catch (Exception ex) {
  44.             return "Error";
  45.         }
  46.     }
  47. }
RAW Paste Data