diff options
author | snowleo <schneeleo@gmail.com> | 2011-10-12 03:14:07 +0200 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2011-10-12 03:14:26 +0200 |
commit | ea192ddd6d5b538e0bd0f6f1721890eb3c25de30 (patch) | |
tree | 344d5ad4eec389eedc6a72dbfb1bdfba1047a447 /EssentialsUpdate/src/com/earth2me/essentials/update/PostToUrl.java | |
parent | 1b34ed40736b03f440352f8193f24b6b0525868a (diff) | |
download | Essentials-ea192ddd6d5b538e0bd0f6f1721890eb3c25de30.tar Essentials-ea192ddd6d5b538e0bd0f6f1721890eb3c25de30.tar.gz Essentials-ea192ddd6d5b538e0bd0f6f1721890eb3c25de30.tar.lz Essentials-ea192ddd6d5b538e0bd0f6f1721890eb3c25de30.tar.xz Essentials-ea192ddd6d5b538e0bd0f6f1721890eb3c25de30.zip |
EssentialsUpdate WIP
Diffstat (limited to 'EssentialsUpdate/src/com/earth2me/essentials/update/PostToUrl.java')
-rw-r--r-- | EssentialsUpdate/src/com/earth2me/essentials/update/PostToUrl.java | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/PostToUrl.java b/EssentialsUpdate/src/com/earth2me/essentials/update/PostToUrl.java new file mode 100644 index 000000000..c8978961b --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/PostToUrl.java @@ -0,0 +1,66 @@ +package com.earth2me.essentials.update; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.math.BigInteger; +import java.net.URL; +import java.net.URLConnection; +import java.nio.charset.Charset; +import java.util.Map; +import java.util.Random; + + +public class PostToUrl +{ + private final transient URL url; + private final transient String boundary; + private final transient Random random = new Random(); + private final static String CRLF = "\r\n"; + private final static Charset UTF8 = Charset.forName("utf-8"); + + public PostToUrl(final URL url) + { + this.url = url; + final byte[] bytes = new byte[32]; + random.nextBytes(bytes); + this.boundary = "----------" + new BigInteger(bytes).toString(Character.MAX_RADIX) + "_$"; + } + + public String send(final Map<String, Object> data) throws IOException + { + final URLConnection connection = url.openConnection(); + connection.setRequestProperty("content-type", "multipart/form-data; boundary=" + boundary); + final StringBuilder dataBuilder = new StringBuilder(); + for (Map.Entry<String, Object> entry : data.entrySet()) + { + if (entry.getValue() instanceof String) + { + dataBuilder.append("--").append(boundary).append(CRLF); + dataBuilder.append("Content-Disposition: form-data; name=\"").append(entry.getKey()).append('"').append(CRLF); + dataBuilder.append(CRLF); + dataBuilder.append(entry.getValue()).append(CRLF); + } + // TODO: Add support for file upload + } + dataBuilder.append("--").append(boundary).append("--").append(CRLF); + dataBuilder.append(CRLF); + connection.setDoOutput(true); + final byte[] message = dataBuilder.toString().getBytes(UTF8); + connection.setRequestProperty("content-length", Integer.toString(message.length)); + connection.connect(); + final OutputStream stream = connection.getOutputStream(); + stream.write(message); + stream.close(); + final BufferedReader page = new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF8)); + final StringBuilder input = new StringBuilder(); + String line; + while ((line = page.readLine()) != null) + { + input.append(line).append("\n"); + } + page.close(); + return input.toString(); + } +} |