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.classic.methods.HttpHead;
32 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
33 import org.apache.hc.client5.http.impl.classic.HttpClients;
34 import org.apache.hc.client5.http.protocol.HttpClientContext;
35 import org.apache.hc.core5.http.EndpointDetails;
36 import org.apache.hc.core5.http.io.entity.EntityUtils;
37 import org.apache.hc.core5.http.message.StatusLine;
38
39
40
41
42 public class ClientRemoteEndpointDetails {
43
44 public static void main(final String[] args) throws Exception {
45 try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
46
47 final HttpClientContext localContext = HttpClientContext.create();
48
49 final HttpGet httpget = new HttpGet("http://httpbin.org/get");
50 System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
51
52
53 httpclient.execute(httpget, localContext, response -> {
54 System.out.println("----------------------------------------");
55 System.out.println(httpget + "->" + new StatusLine(response));
56 EntityUtils.consume(response.getEntity());
57
58 final EndpointDetails endpointDetails = localContext.getEndpointDetails();
59 System.out.println("Remote address: " + endpointDetails.getRemoteAddress());
60 System.out.println("Request counts: " + endpointDetails.getRequestCount());
61 System.out.println("Response counts: " + endpointDetails.getResponseCount());
62 System.out.println("Bytes sent: " + endpointDetails.getSentBytesCount());
63 System.out.println("Bytes received: " + endpointDetails.getReceivedBytesCount());
64
65 return null;
66 });
67
68
69
70
71 final HttpHead httphead = new HttpHead("http://httpbin.org/get");
72 System.out.println("Executing request " + httphead.getMethod() + " " + httphead.getUri());
73
74
75 httpclient.execute(httphead, localContext, response -> {
76 System.out.println("----------------------------------------");
77 System.out.println(httphead + "->" + new StatusLine(response));
78 EntityUtils.consume(response.getEntity());
79
80 final EndpointDetails endpointDetails = localContext.getEndpointDetails();
81 System.out.println("Remote address: " + endpointDetails.getRemoteAddress());
82 System.out.println("Request counts: " + endpointDetails.getRequestCount());
83 System.out.println("Response counts: " + endpointDetails.getResponseCount());
84 System.out.println("Bytes sent: " + endpointDetails.getSentBytesCount());
85 System.out.println("Bytes received: " + endpointDetails.getReceivedBytesCount());
86
87 return null;
88 });
89 }
90 }
91
92 }
93