/net/minecraft/server/HttpUtilities.java

https://bitbucket.org/agaricusb/mc-dev · Java · 89 lines · 72 code · 17 blank · 0 comment · 7 complexity · f97005fb3e28942a71da8a643c276477 MD5 · raw file

  1. package net.minecraft.server;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.net.URLEncoder;
  9. import java.util.Iterator;
  10. import java.util.Map;
  11. import java.util.Map.Entry;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. public class HttpUtilities {
  15. public static String a(Map map) {
  16. StringBuilder stringbuilder = new StringBuilder();
  17. Iterator iterator = map.entrySet().iterator();
  18. while (iterator.hasNext()) {
  19. Entry entry = (Entry) iterator.next();
  20. if (stringbuilder.length() > 0) {
  21. stringbuilder.append('&');
  22. }
  23. try {
  24. stringbuilder.append(URLEncoder.encode((String) entry.getKey(), "UTF-8"));
  25. } catch (UnsupportedEncodingException unsupportedencodingexception) {
  26. unsupportedencodingexception.printStackTrace();
  27. }
  28. if (entry.getValue() != null) {
  29. stringbuilder.append('=');
  30. try {
  31. stringbuilder.append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
  32. } catch (UnsupportedEncodingException unsupportedencodingexception1) {
  33. unsupportedencodingexception1.printStackTrace();
  34. }
  35. }
  36. }
  37. return stringbuilder.toString();
  38. }
  39. public static String a(URL url, Map map, boolean flag) {
  40. return a(url, a(map), flag);
  41. }
  42. public static String a(URL url, String s, boolean flag) {
  43. try {
  44. HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection();
  45. httpurlconnection.setRequestMethod("POST");
  46. httpurlconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  47. httpurlconnection.setRequestProperty("Content-Length", "" + s.getBytes().length);
  48. httpurlconnection.setRequestProperty("Content-Language", "en-US");
  49. httpurlconnection.setUseCaches(false);
  50. httpurlconnection.setDoInput(true);
  51. httpurlconnection.setDoOutput(true);
  52. DataOutputStream dataoutputstream = new DataOutputStream(httpurlconnection.getOutputStream());
  53. dataoutputstream.writeBytes(s);
  54. dataoutputstream.flush();
  55. dataoutputstream.close();
  56. BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(httpurlconnection.getInputStream()));
  57. StringBuffer stringbuffer = new StringBuffer();
  58. String s1;
  59. while ((s1 = bufferedreader.readLine()) != null) {
  60. stringbuffer.append(s1);
  61. stringbuffer.append('\r');
  62. }
  63. bufferedreader.close();
  64. return stringbuffer.toString();
  65. } catch (Exception exception) {
  66. if (!flag) {
  67. Logger.getLogger("Minecraft").log(Level.SEVERE, "Could not post to " + url, exception);
  68. }
  69. return "";
  70. }
  71. }
  72. }