This is a http client program which can be used with another program that requires a (simple) client implementation to call an API.
Feel free to give any feedback.
- Create an instance of
HttpClient
orHttpsClient
class and assign it toClient
class. - The setter methods can be chained to the Client object.
- Call the request() method of the client object to make the request and get the response.
Client client = new HttpClient("www.google.com").setMethod("GET");
Response response = client.request();
System.out.println(response.getText());
Note:-
- If the HTTP method is not set, then GET method will be used by default.
- Auto redirect is still not implemented.
1. GET request:
Response response = SimpleClient.get("url").request();
2. GET request with some headers:
Response response = SimpleClient.get("url")
.setHeader(headersMap) // to all the headers at once
.addHeader(key, value) // headers can be added one by one
.request();
3. POST request:
Response response = SimpleClient.post("url", body).request();
4. Any other methods:
Response response = SimpleClient.request(method, url)
.keepConnectionOpen(false)
.followRedirect(false)
.setMethod("POST")
.setPath("/")
.setBody(new byte[0])
.connectionTimeout(Duration.ofSeconds(10))
.request();
public class Main
{
public static void main(String[] args) throws IOException
{
System.out.println("API 1: GET using SimpleClient");
Response response1 = SimpleClient.get("https://www.google.com").request();
System.out.println(response1.getStatusCode() + ": " + response1.getStatusText());
System.out.println(response1.getText());
System.out.println("API 2: POST using SimpleClient");
Response response2 = SimpleClient.post("http://calapi.inadiutorium.cz/api/v0/en/calendars", "This is http body".getBytes(StandardCharsets.UTF_8)).request();
System.out.println(response2.getStatusCode() + ": " + response2.getStatusText());
System.out.println(response2.getText());
System.out.println("API 3: POST using SimpleClient without body");
Response response3 = SimpleClient.post("", null).request();
System.out.println(response3.getStatusCode() + ": " + response3.getStatusText());
System.out.println(response3.getText());
System.out.println("API 4: GET using HttpClient with one header");
Response response4 = new HttpClient("ip address / host name")
.addHeader("Host", "host name") // adding host to the header is mandatory as per the http specification
.request();
System.out.println(response4.getStatusCode() + ": " + response4.getStatusText());
System.out.println(new String(response4.getBody()));
System.out.println("API 5: POST using HttpsClient with all parameters");
Response response5 = new HttpsClient("ip address / host", 443)
.keepConnectionOpen(false)
.followRedirects(false)
.setMethod("POST")
.setPath("/")
.setParameters(new HashMap<>()) // to add all the parameters at once
.addParameter("key", "value") // parameters can be added one by one
.setHeader(new HashMap<>()) // to all the headers at once
.addHeader("key", "value") // headers can be added one by one
.setBody(new byte[0])
.connectionTimeout(Duration.ofSeconds(20))
.request();
System.out.println(response5.getVersion() + " " + response5.getStatusCode() + ": " + response5.getStatusText());
response5.getHeaders().forEach((String key, String value) -> System.out.println(key + ": " + value));
System.out.println(response5.getText());
}
}