aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/network
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/torui/coflsky/network')
-rw-r--r--src/main/java/de/torui/coflsky/network/QueryServerCommands.java49
1 files changed, 44 insertions, 5 deletions
diff --git a/src/main/java/de/torui/coflsky/network/QueryServerCommands.java b/src/main/java/de/torui/coflsky/network/QueryServerCommands.java
index 0bf54a3..67f8f2a 100644
--- a/src/main/java/de/torui/coflsky/network/QueryServerCommands.java
+++ b/src/main/java/de/torui/coflsky/network/QueryServerCommands.java
@@ -1,9 +1,6 @@
package de.torui.coflsky.network;
-import java.io.BufferedInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
@@ -12,6 +9,8 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import de.torui.coflsky.CoflSky;
+import de.torui.coflsky.minecraft_integration.CoflSessionManager;
+import de.torui.coflsky.minecraft_integration.PlayerDataProvider;
public class QueryServerCommands {
@@ -37,7 +36,7 @@ public class QueryServerCommands {
}
- return "§4ERROR: Could not connect to command server!";
+ return "§4ERROR: Could not connect to command server!";
}
private static class CommandInfo {
@@ -102,4 +101,44 @@ public class QueryServerCommands {
return null;
}
+ public static String PostRequest(String uri, String data) {
+ try {
+ String username = PlayerDataProvider.getUsername();
+ URL url = new URL(uri);
+ HttpURLConnection con;
+ con = (HttpURLConnection) url.openConnection();
+ con.setRequestMethod("POST");
+
+ con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
+ con.setRequestProperty("Accept", "application/json");
+ con.setRequestProperty("User-Agent", "CoflMod");
+ con.setRequestProperty("conId", CoflSessionManager.GetCoflSession(username).SessionUUID);
+ con.setRequestProperty("uuid",username);
+ con.setDoInput(true);
+ con.setDoOutput(true);
+ // ...
+
+ OutputStream os = con.getOutputStream();
+ byte[] bytes = data.getBytes("UTF-8");
+ os.write(bytes);
+ os.close();
+
+ InputStream in = new BufferedInputStream(con.getInputStream());
+ ByteArrayOutputStream result = new ByteArrayOutputStream();
+ byte[] buffer = new byte[1024];
+ for (int length; (length = in.read(buffer)) != -1; ) {
+ result.write(buffer, 0, length);
+ }
+ // StandardCharsets.UTF_8.name() > JDK 7
+ String resString = result.toString("UTF-8");
+
+ System.out.println("Result= " + resString);
+ return resString;
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return null;
+ }
}