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.host; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; /** * * @author Wouter */ public class MagisterRequest { private String subUrl; private HashMap<String, String> headersOut; private String bodyOut; private RequestType reqOut; private HashMap<String, String> headersIn; private String bodyIn; private int responseCodeIn; private static String contentType = "application/json"; private static String userAgent = "Mozilla/5.0"; public MagisterRequest(String subUrl, RequestType type, String body) { this.subUrl = subUrl; this.reqOut = type; this.bodyOut = body; this.headersOut = new HashMap<String, String>(); this.headersIn = new HashMap<String, String>(); this.bodyIn = ""; this.responseCodeIn = -1; } public MagisterRequest(String subUrl, RequestType type, JSONObject obj) { this(subUrl, type, obj.toJSONString()); } public MagisterRequest(String subUrl, RequestType type) { this(subUrl, type, ""); } public MagisterRequest(String subUrl) { this(subUrl, RequestType.GET); } public void addHeader(String key, String value) { this.headersOut.put(key, value); } public HashMap<String, String> getRequestHeaders() { return this.headersIn; } public HashMap<String, String> getResponseHeaders() { return this.headersOut; } public void setCookie(String key, String value) { if (getRequestHeaders().containsKey("Cookie")) { String cookie = getRequestHeaders().get("Cookie"); cookie += "; " + key + "=" + value; getRequestHeaders().put("Cookie", cookie); } else { String cookie = key + "=" + value; getRequestHeaders().put("Cookie", cookie); } } public HashMap<String, String> getSetCookies() { if (this.headersIn.size() > 0) { if (headersIn.containsKey("Set-Cookie")) { String c = headersIn.get("Set-Cookie"); String[] cookies = new String[0]; if (c.contains(";")) { cookies = c.split(";"); } else { cookies = new String[] { c }; } HashMap<String, String> cks = new HashMap<String, String>(); for (String s : cookies) { if (s.contains("=")) { cks.put(s.split("=")[0].trim(), s.split("=")[1].trim()); } } return cks; } } return new HashMap<String, String>(); } public void sendRequest(MagisterHost host) throws MalformedURLException, IOException { URL obj = new URL(host.getApiHost() + subUrl); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod(reqOut.toString()); con.setRequestProperty("User-Agent", userAgent); con.setRequestProperty("Content-Type", contentType); for (Entry<String, String> e : this.headersIn.entrySet()) { con.setRequestProperty(e.getKey(), e.getValue()); } if (reqOut != RequestType.GET && this.bodyOut.length() > 0) { con.setDoOutput(true); con.getOutputStream().write(this.bodyOut.getBytes("UTF-8")); } this.responseCodeIn = con.getResponseCode(); System.out.println("\nSending '" + reqOut.toString() + "' request to URL : " + obj.getPath()); System.out.println("Response Code : " + this.responseCodeIn); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); this.headersIn.clear(); for (String key : con.getHeaderFields().keySet()) { this.headersIn.put(key, con.getHeaderField(key)); } this.bodyIn = response.toString(); } public String getBodyReceived() { return bodyIn; } public JSONObject getJSONReceived() throws Exception { return (JSONObject) new JSONParser().parse(this.bodyIn); } public int getResponseCode() { return responseCodeIn; } }