33 * To change this template file, choose Tools | Templates
44 * and open the template in the editor.
55 */
6- package com .baeldung .java9 . httpclient ;
6+ package com .baeldung .httpclient ;
77
88import java .io .File ;
99import java .io .IOException ;
2525 * @author pkaria
2626 */
2727public class HttpClientExample {
28+
29+ public static void main (String [] args ) throws Exception {
30+ httpGetRequest ();
31+ httpPosttRequest ();
32+ asynchronousRequest ();
33+ asynchronousMultipleRequests ();
34+ }
2835
29- public void httpGetRequest () throws URISyntaxException , IOException , InterruptedException {
36+ public static void httpGetRequest () throws URISyntaxException , IOException , InterruptedException {
3037 HttpClient client = HttpClient .newHttpClient ();
3138 URI httpURI = new URI ("http://jsonplaceholder.typicode.com/posts/1" );
3239 HttpRequest request = HttpRequest .newBuilder (httpURI ).GET ().build ();
@@ -36,7 +43,7 @@ public void httpGetRequest() throws URISyntaxException, IOException, Interrupted
3643 System .out .println (responseBody );
3744 }
3845
39- public void httpPosttRequest () throws URISyntaxException , IOException , InterruptedException {
46+ public static void httpPosttRequest () throws URISyntaxException , IOException , InterruptedException {
4047 HttpClient client = HttpClient
4148 .newBuilder ()
4249 .build ();
@@ -50,15 +57,15 @@ public void httpPosttRequest() throws URISyntaxException, IOException, Interrupt
5057 System .out .println (responseBody );
5158 }
5259
53- public void asynchronousRequest () throws URISyntaxException {
60+ public static void asynchronousRequest () throws URISyntaxException {
5461 HttpClient client = HttpClient .newHttpClient ();
5562 URI httpURI = new URI ("http://jsonplaceholder.typicode.com/posts/1" );
5663 HttpRequest request = HttpRequest .newBuilder (httpURI ).GET ().build ();
5764 CompletableFuture <HttpResponse <String >> futureResponse = client .sendAsync (request ,
5865 HttpResponse .BodyHandler .asString ());
5966 }
6067
61- public void asynchronousMultipleRequests () throws URISyntaxException {
68+ public static void asynchronousMultipleRequests () throws URISyntaxException {
6269 List <URI > targets = Arrays .asList (new URI ("http://jsonplaceholder.typicode.com/posts/1" ), new URI ("http://jsonplaceholder.typicode.com/posts/2" ));
6370 HttpClient client = HttpClient .newHttpClient ();
6471 List <CompletableFuture <File >> futures = targets
0 commit comments