Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. package plugins.remotejava;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.UnsupportedEncodingException;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. import java.net.URLConnection;
  12. import java.net.URLEncoder;
  13. import java.util.Map;
  14. import java.util.Set;
  15.  
  16. public class HttpUtils
  17. {
  18.     public static String excutePost(String targetURL,
  19.             Map<String, String> urlParameters) throws IOException
  20.     {
  21.         if (urlParameters == null)
  22.         {
  23.             throw new IllegalArgumentException("urlParameters cannot be null.");
  24.         }
  25.  
  26.         Set<String> urlKeys = urlParameters.keySet();
  27.         StringBuilder sbr = new StringBuilder();
  28.  
  29.         for (String s : urlKeys)
  30.         {
  31.             try
  32.             {
  33.                 sbr.append((s == urlKeys.iterator().next() ? s + "="
  34.                         + URLEncoder.encode(urlParameters.get(s), "UTF-8")
  35.                         : "&"
  36.                                 + s
  37.                                 + "="
  38.                                 + URLEncoder.encode(urlParameters.get(s),
  39.                                         "UTF-8")));
  40.             } catch (UnsupportedEncodingException e)
  41.             {
  42.                 System.err.println(":(. Get a UTF-8.");
  43.             }
  44.         }
  45.  
  46.         URL url;
  47.         HttpURLConnection connection = null;
  48.         try
  49.         {
  50.             // Create connection
  51.             url = new URL(targetURL);
  52.             URLConnection urlc = url.openConnection();
  53.             if (!(urlc instanceof HttpURLConnection))
  54.             {
  55.                 throw new IllegalArgumentException("URL protocol must be HTTP.");
  56.             }
  57.             connection = (HttpURLConnection) urlc;
  58.             connection.setRequestMethod("POST");
  59.             connection.setRequestProperty("Content-Type",
  60.                     "application/x-www-form-urlencoded");
  61.  
  62.             connection.setRequestProperty("Content-Length",
  63.                     "" + Integer.toString(sbr.toString().getBytes().length));
  64.             connection.setRequestProperty("Content-Language", "en-US");
  65.  
  66.             connection.setUseCaches(false);
  67.             connection.setDoInput(true);
  68.             connection.setDoOutput(true);
  69.  
  70.             // Send request
  71.             DataOutputStream wr = new DataOutputStream(
  72.                     connection.getOutputStream());
  73.             wr.writeBytes(sbr.toString());
  74.             wr.flush();
  75.             wr.close();
  76.  
  77.             // Get Response
  78.             InputStream is = connection.getInputStream();
  79.             BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  80.             String line;
  81.             StringBuffer response = new StringBuffer();
  82.             while ((line = rd.readLine()) != null)
  83.             {
  84.                 response.append(line);
  85.                 response.append('\r');
  86.             }
  87.             rd.close();
  88.             return response.toString();
  89.  
  90.         } finally
  91.         {
  92.  
  93.             if (connection != null)
  94.             {
  95.                 connection.disconnect();
  96.             }
  97.         }
  98.     }
  99.  
  100.     public static String getPage(String pageUrl) throws IOException
  101.     {
  102.  
  103.         URL url;
  104.         HttpURLConnection connection = null;
  105.         try
  106.         {
  107.             // Create connection
  108.             url = new URL(pageUrl);
  109.             URLConnection urlc = url.openConnection();
  110.             if (!(urlc instanceof HttpURLConnection))
  111.             {
  112.                 throw new IllegalArgumentException("URL protocol must be HTTP.");
  113.             }
  114.             connection = (HttpURLConnection) urlc;
  115.  
  116.             connection.connect();
  117.  
  118.             // Get Response
  119.             InputStream is = connection.getInputStream();
  120.             BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  121.             String line;
  122.             StringBuffer response = new StringBuffer();
  123.             while ((line = rd.readLine()) != null)
  124.             {
  125.                 response.append(line);
  126.                 response.append('\r');
  127.             }
  128.             rd.close();
  129.             return response.toString();
  130.  
  131.         } finally
  132.         {
  133.  
  134.             if (connection != null)
  135.             {
  136.                 connection.disconnect();
  137.             }
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement