Skip to content

Instantly share code, notes, and snippets.

@Fysac
Last active August 29, 2015 14:03
Show Gist options
  • Save Fysac/7d92d919ea5bbdefdbb7 to your computer and use it in GitHub Desktop.
Save Fysac/7d92d919ea5bbdefdbb7 to your computer and use it in GitHub Desktop.
/* Fixed - I am an idiot!
The JSON selector "serverID" (line 12) should be "serverId".
Then remove "shouldRead" from the whole equation.
Bugged code preserved below for posterity.
*/
@SuppressWarnings("unchecked")
public void sendSessionRequest(String serverIdHash) throws IOException {
JSONObject payload = new JSONObject();
payload.put("accessToken", accessToken);
payload.put("selectedProfile", uuid);
payload.put("serverID", serverIdHash);
System.out.println("Payload: " + payload.toString());
sendJSONPostRequest("https://sessionserver.mojang.com/session/minecraft/join", payload, false);
}
/* This is what the above method sends:
{"selectedProfile":"e3081eaf42e24a7ca4c35692d130dcd2","accessToken":"efc9ae0198db4f0c9f74bae745661478","serverID":"2ed8215335aa1b01ecd758de840f708c56e47311"}
*/
public static String sendJSONPostRequest(String url, JSONObject data, boolean shouldRead) throws IOException {
String resp = "";
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("content-type", "application/json; charset=utf-8");
conn.setUseCaches(false);
conn.setRequestMethod("POST");
OutputStreamWriter w = new OutputStreamWriter(conn.getOutputStream());
w.write(data.toJSONString());
w.flush();
// I added this boolean because sendSessionRequest() doesn't seem to receive a response from the server.
if (shouldRead){
InputStream s;
s = conn.getInputStream();
int i = s.read();
while (i != -1){
resp += (char) i;
i = s.read();
}
}
conn.disconnect();
return resp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment