{"id":1156,"date":"2020-03-25T12:31:25","date_gmt":"2020-03-24T23:31:25","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1156"},"modified":"2021-09-28T14:01:46","modified_gmt":"2021-09-28T01:01:46","slug":"sample-httpclient-sendasync-method-usage","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1156","title":{"rendered":"Sample HttpClient.sendAsync() Method Usage"},"content":{"rendered":"<pre style=\"white-space: pre;\">\/\/Create an instance of HttpClient using its builder.\r\nHttpClient httpClient = HttpClient.newBuilder()\r\n        .version(HttpClient.Version.HTTP_2)\r\n        .build();\r\n\r\n\/\/Create an instance of HttpRequest using its builder.\r\nHttpRequest req = HttpRequest.newBuilder(URI.create(\"https:\/\/www.google.com\"))\r\n            .GET()\r\n            .build();\r\n\r\n\/* Use the httpClient.sendAsync() method and encapsulate the response\r\nin the CompletableFuture. *\/\r\nCompletableFuture&lt;HttpResponse&gt; resFuture =\r\n        httpClient.sendAsync(req, HttpResponse.BodyHandlers.ofString());\r\n\r\n\/\/Use the resFuture.thenAccept to wait for the async response.\r\nresFuture.thenAccept(res -&gt; System.out.println(res.version()));\r\n\r\n\/\/Wait for the resFuture to to complete.\r\nresFuture.join();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/Create an instance of HttpClient using its builder. HttpClient httpClient = HttpClient.newBuilder() .version(HttpClient.Version.HTTP_2) .build(); \/\/Create an instance of HttpRequest using its builder. HttpRequest req = HttpRequest.newBuilder(URI.create(&#8220;https:\/\/www.google.com&#8221;)) .GET() .build(); \/* Use the httpClient.sendAsync() method and encapsulate the response in the CompletableFuture. *\/ CompletableFuture&lt;HttpResponse&gt; resFuture = httpClient.sendAsync(req, HttpResponse.BodyHandlers.ofString()); \/\/Use the resFuture.thenAccept to wait for the async response. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[62,71],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1156"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1156"}],"version-history":[{"count":1,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1156\/revisions"}],"predecessor-version":[{"id":1157,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1156\/revisions\/1157"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}