Last active
October 4, 2017 20:00
-
-
Save jdesive/206b7119ae09da5e9bf2784c1e98eabc to your computer and use it in GitHub Desktop.
A Request class for java to make Http calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.Map; | |
public class Request { | |
public static String HOSTADDRESS = "http://localhost:8080"; | |
public static String get(String target, Map<String, String> parameters, Map<String, String> headers) throws IOException{ | |
return request(target, parameters, headers, null, "GET"); | |
} | |
public static String get(String target, Map<String, String> parameters) throws IOException{ | |
return request(target, parameters, null, null, "GET"); | |
} | |
public static String get(String target) throws IOException{ | |
return request(target, null, null, null, "GET"); | |
} | |
public static String post(String target, String body, Map<String, String> parameters, Map<String, String> headers) throws IOException{ | |
return request(target, parameters, headers, body, "POST"); | |
} | |
public static String post(String target, String body, Map<String, String> parameters) throws IOException{ | |
return request(target, parameters, null, body, "POST"); | |
} | |
public static String post(String target, String body) throws IOException{ | |
return request(target, null, null, body, "POST"); | |
} | |
public static String put(String target, String body, Map<String, String> parameters, Map<String, String> headers) throws IOException{ | |
return request(target, parameters, headers, body, "PUT"); | |
} | |
public static String put(String target, String body, Map<String, String> parameters) throws IOException{ | |
return request(target, parameters, null, body, "PUT"); | |
} | |
public static String put(String target, String body) throws IOException{ | |
return request(target, null, null, body, "PUT"); | |
} | |
public static String delete(String target, Map<String, String> parameters, Map<String, String> headers) throws IOException{ | |
return request(target, parameters, headers, null, "DELETE"); | |
} | |
public static String delete(String target, Map<String, String> parameters) throws IOException{ | |
return request(target, parameters, null, null, "DELETE"); | |
} | |
public static String delete(String target) throws IOException{ | |
return request(target, null, null, null, "DELETE"); | |
} | |
public static String head(String target, Map<String, String> parameters, Map<String, String> headers) throws IOException{ | |
return request(target, parameters, headers, null, "HEAD"); | |
} | |
public static String head(String target, Map<String, String> parameters) throws IOException{ | |
return request(target, parameters, null, null, "HEAD"); | |
} | |
public static String head(String target) throws IOException{ | |
return request(target, null, null, null, "HEAD"); | |
} | |
public static String options(String target, Map<String, String> parameters, Map<String, String> headers) throws IOException{ | |
return request(target, parameters, headers, null, "OPTIONS"); | |
} | |
public static String options(String target, Map<String, String> parameters) throws IOException{ | |
return request(target, parameters, null, null, "OPTIONS"); | |
} | |
public static String options(String target) throws IOException{ | |
return request(target, null, null, null, "OPTIONS"); | |
} | |
public static String patch(String target, String body, Map<String, String> parameters, Map<String, String> headers) throws IOException{ | |
return request(target, parameters, headers, body, "PATCH"); | |
} | |
public static String patch(String target, String body, Map<String, String> parameters) throws IOException{ | |
return request(target, parameters, null, body, "PATCH"); | |
} | |
public static String patch(String target, String body) throws IOException{ | |
return request(target, null, null, body, "PATCH"); | |
} | |
public static String request(String target, Map<String, String> parameters, Map<String, String> headers, String body, String method) throws IOException{ | |
URL targetUrl = new URL(target + createParameterString(parameters)); | |
HttpURLConnection connection = (HttpURLConnection) targetUrl.openConnection(); | |
if(headers != null && !headers.isEmpty()){ | |
headers.forEach(connection::setRequestProperty); | |
} | |
connection.setRequestMethod(method.toUpperCase()); | |
connection.setConnectTimeout(15000); | |
connection.setReadTimeout(15000); | |
if(body != null && body != "") { | |
connection.setDoOutput(true); | |
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); | |
outputStream.writeBytes(body); | |
outputStream.flush(); | |
outputStream.close(); | |
} | |
BufferedReader inputStream = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
String inputLine; | |
StringBuilder response = new StringBuilder(); | |
while ((inputLine = inputStream.readLine()) != null) { | |
response.append(inputLine); | |
} | |
inputStream.close(); | |
connection.disconnect(); | |
return response.toString(); | |
} | |
private static String createParameterString(Map<String, String> parameters){ | |
if(parameters == null || parameters.isEmpty()) | |
return ""; | |
StringBuilder paramString = new StringBuilder("?"); | |
parameters.forEach((k, v) -> { | |
paramString.append(k); | |
paramString.append("="); | |
paramString.append(v); | |
paramString.append("&"); | |
}); | |
paramString.deleteCharAt(paramString.length()-1); | |
return paramString.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment