SHARE
TWEET

Untitled

a guest Feb 18th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.b5team.postrequest;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.PrintStream;
  9. import java.io.UnsupportedEncodingException;
  10. import java.net.HttpURLConnection;
  11. import java.net.MalformedURLException;
  12. import java.net.ProtocolException;
  13. import java.net.URL;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16. import org.bukkit.scheduler.BukkitRunnable;
  17.  
  18. class HttpPOSTRequest
  19. {
  20.   static void sendRequest(String myurl, final String hash, final String[] args)
  21.   {
  22.     BukkitRunnable r = new BukkitRunnable()
  23.     {
  24.       public void run()
  25.       {
  26.         try
  27.         {
  28.           URL url = new URL(this.val$myurl);
  29.          
  30.           HttpURLConnection con = (HttpURLConnection)url.openConnection();
  31.           con.setRequestMethod("POST");
  32.           con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  33.           con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)");
  34.           con.setDoOutput(true);
  35.           con.setDoInput(true);
  36.          
  37.           DataOutputStream output = new DataOutputStream(con.getOutputStream());
  38.           output.writeBytes("hash=" + hash);
  39.           for (int i = 0; i < args.length; i++)
  40.           {
  41.             output.writeBytes("&");
  42.             output.writeBytes("arg" + i + "=" + args[i]);
  43.             output.flush();
  44.           }
  45.           output.flush();
  46.           output.close();
  47.          
  48.           DataInputStream input = new DataInputStream(con.getInputStream());
  49.           BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  50.          
  51.           Main.getMainLogger().info("Data sent successfully!");
  52.           Main.getMainLogger().info("Resp Code:" + con.getResponseCode());
  53.           System.out.println("Resp Message:" + con.getResponseMessage());
  54.           String line;
  55.           while ((line = reader.readLine()) != null) {
  56.             Main.getMainLogger().info("Report: " + line);
  57.           }
  58.           input.close();
  59.         }
  60.         catch (UnsupportedEncodingException e)
  61.         {
  62.           Main.getMainLogger().log(Level.SEVERE, "Encoding error. Maybe string have invalid caracters.", e);
  63.         }
  64.         catch (MalformedURLException e)
  65.         {
  66.           Main.getMainLogger().log(Level.SEVERE, "Invalid URL. Verify your URL and try again.", e);
  67.         }
  68.         catch (ProtocolException e)
  69.         {
  70.           Main.getMainLogger().log(Level.SEVERE, "Error on HttpPOST request.", e);
  71.         }
  72.         catch (IOException e)
  73.         {
  74.           Main.getMainLogger().log(Level.SEVERE, "Error on HTTP connection.", e);
  75.         }
  76.       }
  77.     };
  78.     r.runTaskAsynchronously(Main.getInstance());
  79.   }
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top