1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.examples;
29
30 import org.apache.hc.client5.http.classic.methods.HttpGet;
31 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
32 import org.apache.hc.client5.http.impl.classic.HttpClients;
33 import org.apache.hc.core5.http.io.entity.EntityUtils;
34 import org.apache.hc.core5.http.message.StatusLine;
35
36
37
38
39
40 public class ClientResponseProcessing {
41
42 public static void main(final String[] args) throws Exception {
43 try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
44 final HttpGet httpget = new HttpGet("http://httpbin.org/get");
45
46 System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
47
48 final Result result = httpclient.execute(httpget, response -> {
49 System.out.println("----------------------------------------");
50 System.out.println(httpget + "->" + new StatusLine(response));
51
52 return new Result(response.getCode(), EntityUtils.toString(response.getEntity()));
53 });
54 System.out.println(result);
55 }
56 }
57
58 static class Result {
59
60 final int status;
61 final String content;
62
63 Result(final int status, final String content) {
64 this.status = status;
65 this.content = content;
66 }
67
68 }
69
70 }
71