/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
- package net.minecraft.server;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class HttpUtilities {
- public static String a(Map map) {
- StringBuilder stringbuilder = new StringBuilder();
- Iterator iterator = map.entrySet().iterator();
- while (iterator.hasNext()) {
- Entry entry = (Entry) iterator.next();
- if (stringbuilder.length() > 0) {
- stringbuilder.append('&');
- }
- try {
- stringbuilder.append(URLEncoder.encode((String) entry.getKey(), "UTF-8"));
- } catch (UnsupportedEncodingException unsupportedencodingexception) {
- unsupportedencodingexception.printStackTrace();
- }
- if (entry.getValue() != null) {
- stringbuilder.append('=');
- try {
- stringbuilder.append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
- } catch (UnsupportedEncodingException unsupportedencodingexception1) {
- unsupportedencodingexception1.printStackTrace();
- }
- }
- }
- return stringbuilder.toString();
- }
- public static String a(URL url, Map map, boolean flag) {
- return a(url, a(map), flag);
- }
- public static String a(URL url, String s, boolean flag) {
- try {
- HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection();
- httpurlconnection.setRequestMethod("POST");
- httpurlconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- httpurlconnection.setRequestProperty("Content-Length", "" + s.getBytes().length);
- httpurlconnection.setRequestProperty("Content-Language", "en-US");
- httpurlconnection.setUseCaches(false);
- httpurlconnection.setDoInput(true);
- httpurlconnection.setDoOutput(true);
- DataOutputStream dataoutputstream = new DataOutputStream(httpurlconnection.getOutputStream());
- dataoutputstream.writeBytes(s);
- dataoutputstream.flush();
- dataoutputstream.close();
- BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(httpurlconnection.getInputStream()));
- StringBuffer stringbuffer = new StringBuffer();
- String s1;
- while ((s1 = bufferedreader.readLine()) != null) {
- stringbuffer.append(s1);
- stringbuffer.append('\r');
- }
- bufferedreader.close();
- return stringbuffer.toString();
- } catch (Exception exception) {
- if (!flag) {
- Logger.getLogger("Minecraft").log(Level.SEVERE, "Could not post to " + url, exception);
- }
- return "";
- }
- }
- }