How to make a Post Json rpc using java.net

How to make a Post Json rpc using java.net



I have to make a https post to a json-rpc api which wants to have a variable in the url. In this case:


https://mese.webuntis.com/WebUntis/jsonrpc.do?school=GS+T%C3%BCbingen



(this is a public server not my own so hack away).
The Server only accepts https and refuse to do anything without the Schoolname(GS+T%C3%BCbingen):


"jsonrpc":"2.0","id":"error","error":"message":"invalid schoolname","code":-8500



Additionally want to use the default java.net library.



I Got it to accept the Schoolname with this response:


"jsonrpc":"2.0","id":null,"error":"code":-32700,"message":"Parse error: No content to map due to end-of-inputn at [Source: org.apache.catalina.connector.CoyoteInputStream@396fcdd0; line: 1, column: 0]"



Now i have to add the Json data to my post but when i try to. It gives me the inavid schoolname response agin.



I tryed a few thing but im bad at java, here is my current code.


public class Main {

private final String USER_AGENT = "Mozilla/5.0";


public static void main(String args)

Main http = new Main();
String output;
String data;
String dataEncoded;
String schoolName;
String url;

data = ""id":"ID","method":"authenticate","params":"user":"ANDROID","password":"PASSWORD", "client":"CLIENT","jsonrpc":"2.0"";
dataEncoded = "%7B%22id%22%3A%22ID%22%2C%22method%22%3A%22authenticate%22%2C%22params%22%3A%7B%22user%22%3A%22ANDROID%22%2C%20%20%22password%22%3A%22PASSWORD%22%2C%20%22client%22%3A%22CLIENT%22%7D%2C%22jsonrpc%22%3A%222.0%22%7D";
schoolName = "GS+T%C3%BCbingen";// gs tübingen encoded for http
url = "https://mese.webuntis.com/WebUntis/jsonrpc.do";

System.out.println("nTrying Https Post to: " + url + " with SchoolName: " + schoolName + " with data: " + data);
output = http.sendPost(url,schoolName,data);
System.out.println("---Response Start---n" + output + "n---Response End---");




private String sendPost(String url,String schoolName,String data)
try
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
//con.setRequestProperty("Content-Type", "application/json-rpc");
//con.setRequestProperty("", data);

String urlParameters = "school=" + schoolName ;//+ "&" + data;

con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();


BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null)
response.append(inputLine);

in.close();
return response.toString();

catch(IOException e)
return "sensPost() failed: "+ e.getMessage();





I found out that i have to insert the json data into the html body but i have no idea how to and if that data also has to be ascii-% encoded.(Sorry for my mesy code but this is just for testing befor proper implementation.)



-Sammy




0



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)