Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package plugins.remotejava;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLEncoder;
- import java.util.Map;
- import java.util.Set;
- public class HttpUtils
- {
- public static String excutePost(String targetURL,
- Map<String, String> urlParameters) throws IOException
- {
- if (urlParameters == null)
- {
- throw new IllegalArgumentException("urlParameters cannot be null.");
- }
- Set<String> urlKeys = urlParameters.keySet();
- StringBuilder sbr = new StringBuilder();
- for (String s : urlKeys)
- {
- try
- {
- sbr.append((s == urlKeys.iterator().next() ? s + "="
- + URLEncoder.encode(urlParameters.get(s), "UTF-8")
- : "&"
- + s
- + "="
- + URLEncoder.encode(urlParameters.get(s),
- "UTF-8")));
- } catch (UnsupportedEncodingException e)
- {
- System.err.println(":(. Get a UTF-8.");
- }
- }
- URL url;
- HttpURLConnection connection = null;
- try
- {
- // Create connection
- url = new URL(targetURL);
- URLConnection urlc = url.openConnection();
- if (!(urlc instanceof HttpURLConnection))
- {
- throw new IllegalArgumentException("URL protocol must be HTTP.");
- }
- connection = (HttpURLConnection) urlc;
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type",
- "application/x-www-form-urlencoded");
- connection.setRequestProperty("Content-Length",
- "" + Integer.toString(sbr.toString().getBytes().length));
- connection.setRequestProperty("Content-Language", "en-US");
- connection.setUseCaches(false);
- connection.setDoInput(true);
- connection.setDoOutput(true);
- // Send request
- DataOutputStream wr = new DataOutputStream(
- connection.getOutputStream());
- wr.writeBytes(sbr.toString());
- wr.flush();
- wr.close();
- // Get Response
- InputStream is = connection.getInputStream();
- BufferedReader rd = new BufferedReader(new InputStreamReader(is));
- String line;
- StringBuffer response = new StringBuffer();
- while ((line = rd.readLine()) != null)
- {
- response.append(line);
- response.append('\r');
- }
- rd.close();
- return response.toString();
- } finally
- {
- if (connection != null)
- {
- connection.disconnect();
- }
- }
- }
- public static String getPage(String pageUrl) throws IOException
- {
- URL url;
- HttpURLConnection connection = null;
- try
- {
- // Create connection
- url = new URL(pageUrl);
- URLConnection urlc = url.openConnection();
- if (!(urlc instanceof HttpURLConnection))
- {
- throw new IllegalArgumentException("URL protocol must be HTTP.");
- }
- connection = (HttpURLConnection) urlc;
- connection.connect();
- // Get Response
- InputStream is = connection.getInputStream();
- BufferedReader rd = new BufferedReader(new InputStreamReader(is));
- String line;
- StringBuffer response = new StringBuffer();
- while ((line = rd.readLine()) != null)
- {
- response.append(line);
- response.append('\r');
- }
- rd.close();
- return response.toString();
- } finally
- {
- if (connection != null)
- {
- connection.disconnect();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement