make a get request call using httpclient in java

Solutions on MaxInterview for make a get request call using httpclient in java by the best coders in the world

showing results for - "make a get request call using httpclient in java"
Emilia
09 Nov 2017
1package com.zetcode;
2
3import com.fasterxml.jackson.databind.ObjectMapper;
4
5import java.io.IOException;
6import java.net.URI;
7import java.net.http.HttpClient;
8import java.net.http.HttpRequest;
9import java.net.http.HttpResponse;
10import java.util.HashMap;
11
12public class PostRequest {
13
14    public static void main(String[] args) throws IOException, InterruptedException {
15
16        var values = new HashMap<String, String>() {{
17            put("name", "John Doe");
18            put ("occupation", "gardener");
19        }};
20
21        var objectMapper = new ObjectMapper();
22        String requestBody = objectMapper
23                .writeValueAsString(values);
24
25        HttpClient client = HttpClient.newHttpClient();
26        HttpRequest request = HttpRequest.newBuilder()
27                .uri(URI.create("https://httpbin.org/post"))
28                .POST(HttpRequest.BodyPublishers.ofString(requestBody))
29                .build();
30
31        HttpResponse<String> response = client.send(request,
32                HttpResponse.BodyHandlers.ofString());
33
34        System.out.println(response.body());
35    }
36}
Luciano
05 Jan 2021
1package com.zetcode;
2
3import java.io.IOException;
4import java.net.URI;
5import java.net.http.HttpClient;
6import java.net.http.HttpRequest;
7import java.net.http.HttpResponse;
8
9public class GetRequestJava11 {
10
11    public static void main(String[] args) throws IOException, InterruptedException {
12
13        HttpClient client = HttpClient.newHttpClient();
14        HttpRequest request = HttpRequest.newBuilder()
15                .uri(URI.create("http://webcode.me"))
16                .build();
17
18        HttpResponse<String> response = client.send(request,
19                HttpResponse.BodyHandlers.ofString());
20
21        System.out.println(response.body());
22    }
23}
24
queries leading to this page
using httpclient javahttpclientrequest usage in java java net http httpclientjava 6 httpclient get request java example httpclient javajava httpclienthttpclient example javahttpclient java maven examplehttpclient client javahttpclient java downloadjava httpclient to send requestehttpclient library javahttpclient request example javahttpclient put request javahow to call a rest api from java using httpclientjava httpclient mavenconnect api using simple java httpclientjava httpclient get exampleusing httpclient in javahttpclient create javahttpclient get 28 29 method javaget 28 29 in httpclient in java 8java 8 httpclient examplehttpclient get response code javahttpclient java gethttpclient for rest api java projectmake a get request call using httpclient in javajava httpclientshttpclient javadocjava httpclient examplehttpclient class in javahow to use httpclient in javahttpclient java tutorialhttpclient get method javahttpclient java examplecall api using httpclient javahttpclient tutorial javahttpclient in java 11connect api using httpclient javajava get http response httpclienthttpclient java 8java net http httpclient 3b examplejava best httpclient to usehttpclient examples javajava httpclient put requesthttpclient send javagetting with httpclient javahow to use httpclient in java 8java httpclient put exampleuse httpclient in javajava java net http httpclient examplehow to call httpclient in javahttpclient java 8 examplehttpclient package javahttpclient java java use httpclientjava 6 httpclient 4 get request java examplejava httpclient util puthttpclient in java 8httpclient send java examplebest httpclient for javajava httpclient put method examplejava http client httpclient read responsejava httpclient getjava httpclient requesthttpclient java 11 examplehttpclient for java 7reusing httpclient javajava 6httpclient get request java examplehttp client get request java examplehttpclient java example projecthttpclient in java examplehttpclient client 3d new httpclient 28 29 3b javajava rest client httpclienthow add httpclient in javahttpclient in javahttpclient example tutorial javausage of httpclient javajava net http httpclient 3bmake a get request call using httpclient in java