java httpclient example

Solutions on MaxInterview for java httpclient example by the best coders in the world

showing results for - "java httpclient example"
Jemma
26 Aug 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}
Elizabeth
02 Apr 2019
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
httpclient in javahow to use httpclient in java 8http client get request java examplehttpclient java maven examplehttpclient library javamake a get request call using httpclient in javahttpclient send java examplehttpclient java 8 examplehttpclient java httpclient javahow to call httpclient in javajava net http httpclient 3b examplejava 6 httpclient get request java examplehttpclient in java 11java get http response httpclientreusing httpclient javahttpclient request example javahttpclient for java 7java http client httpclient read responsehttpclient in java exampleusing httpclient javause httpclient in javahttpclient get response code javaget 28 29 in httpclient in java 8httpclient get method javahttpclient examples javahttpclient java 11 examplejava net http httpclienthttpclientrequest usage in java httpclient class in javajava 8 httpclient exampleconnect api using simple java httpclientjava rest client httpclientjava httpclient getjava 6 httpclient 4 get request java examplehttpclient put request javahttpclient java examplejava httpclienthttpclient java example projecthttpclient client 3d new httpclient 28 29 3b javajava httpclient to send requestehttpclient client javajava use httpclientjava httpclientsjava httpclient requesthttpclient in java 8getting with httpclient javajava java net http httpclient exampleusage of httpclient javajava httpclient put examplehttpclient example javajava httpclient get examplehow to use httpclient in javahttpclient java 8call api using httpclient javajava 6httpclient get request java examplehttpclient example tutorial javajava httpclient util puthttpclient send javausing httpclient in javajava httpclient examplehttpclient java downloadhttpclient javadochttpclient package javajava net http httpclient 3bjava httpclient mavenjava httpclient put requesthow to call a rest api from java using httpclientbest httpclient for javaconnect api using httpclient javahttpclient java tutorialhttpclient tutorial javahow add httpclient in javahttpclient java getjava best httpclient to usehttpclient create javahttpclient get 28 29 method javajava httpclient put method examplejava httpclient example