Advertisement
aNNiMON

PastebinSender v2

Aug 6th, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  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 POST_URL = "http://pastebin.com/api/api_post.php";
  16.     private static final String USER_LOGIN_URL = "http://pastebin.com/api/api_login.php";
  17.    
  18.    
  19.     /**
  20.      * Send request to create new paste.
  21.      * @param request @see http://pastebin.com/api
  22.      * @return pasted url or error message.
  23.      */
  24.     public static String sendRequest(String request) {
  25.         return sendRequest(request, POST_URL);
  26.     }
  27.    
  28.     /**
  29.      * Get user key in member system.
  30.      * @param request @see http://pastebin.com/api
  31.      * @return user key or error message.
  32.      */
  33.     public static String getUserKey(String request) {
  34.         return sendRequest(request, USER_LOGIN_URL);
  35.     }
  36.    
  37.    
  38.     private static String sendRequest(String request, String connectUrl) {
  39.         try {
  40.             URL url = new URL(connectUrl);
  41.             HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
  42.             httpConn.setRequestMethod("POST");
  43.             httpConn.setInstanceFollowRedirects(false);
  44.             httpConn.setDoOutput(true);
  45.             httpConn.connect();
  46.            
  47.             OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
  48.             out.write(request);
  49.             out.flush();
  50.            
  51.             StringBuilder response = new StringBuilder();
  52.             BufferedReader in = new BufferedReader( new InputStreamReader(httpConn.getInputStream()) );
  53.             String line;
  54.             while ((line = in.readLine()) != null) {
  55.                 response.append(line).append('\n');
  56.             }
  57.             in.close();
  58.             return response.toString();
  59.         } catch (Exception ex) {
  60.             return "Error";
  61.         }
  62.     }
  63.    
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement