1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.newsclub.net.unix.demo.okhttp;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.SocketAddress;
24 import java.time.Duration;
25
26 import org.newsclub.net.unix.AFSocketFactory;
27 import org.newsclub.net.unix.AFUNIXSocketAddress;
28 import org.newsclub.net.unix.demo.DemoHelper;
29 import org.newsclub.net.unix.demo.nanohttpd.NanoHttpdServerDemo;
30
31 import com.kohlschutter.util.IOUtil;
32
33 import okhttp3.OkHttpClient;
34 import okhttp3.Request;
35 import okhttp3.Response;
36 import okhttp3.ResponseBody;
37
38
39
40
41
42
43
44
45
46
47 public class OkHttpClientDemo {
48 public static void main(String[] args) throws IOException {
49 SocketAddress addr = DemoHelper.parseAddress(args,
50 AFUNIXSocketAddress.of(new File("/tmp/junixsocket-http-server.sock")));
51
52 OkHttpClient.Builder builder = new OkHttpClient.Builder()
53 .socketFactory(new AFSocketFactory.FixedAddressSocketFactory(addr))
54 .callTimeout(Duration.ofMinutes(1));
55
56 OkHttpClient client = builder.build();
57
58 Request request = new Request.Builder().url("http://localhost/").build();
59 try (Response response = client.newCall(request).execute()) {
60
61 @SuppressWarnings("resource")
62 ResponseBody body = response.body();
63 if (body != null) {
64 try (InputStream in = body.byteStream()) {
65 IOUtil.transferAllBytes(in, System.out);
66 } finally {
67 body.close();
68 }
69 }
70 }
71 }
72 }