Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package net.sparkeh.magisterlib; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import org.json.simple.JSONObject; /** * * @author BeMacized */ public class MagisterLib { private static String APIURL; private static String USR; private static String PWD; public static String getResponse(String URL) throws Exception { String cookie = getAuthCookie(APIURL, USR, PWD); ArrayList<String> classes = new ArrayList<>(); URL obj = new URL(URL); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //add request header con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Cookie", "SESSION_ID=" + cookie); 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(); } public static String getAuthCookie(String APIUrl, String username, String password) throws Exception { URL obj = new URL(APIUrl + "sessie"); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Cookie", "SESSION_ID=349c6fcb-f49a-47c4-b129-09158986155b"); JSONObject o = new JSONObject(); o.put("GebruikersNaam", username); o.put("Wachtwoord", password); o.put("IngelogdBlijven", false); o.put("GebruikersnaamOnthouden", false); con.setDoOutput(true); con.getOutputStream().write(o.toJSONString().getBytes("UTF-8")); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + obj.getPath()); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); if (con.getHeaderFields().containsKey("Set-Cookie")) { String c = con.getHeaderField("Set-Cookie").toString(); if (c.contains(";")) { String[] s = c.split(";"); for (String k : s) { if (k.contains("SESSION_ID") && k.contains("=")) { return k.split("=")[1].trim(); } } } } return null; } }