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 java.io.IOException;
31 import java.net.InetSocketAddress;
32 import java.net.Proxy;
33 import java.net.Socket;
34
35 import org.apache.hc.client5.http.classic.methods.HttpGet;
36 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
37 import org.apache.hc.client5.http.impl.classic.HttpClients;
38 import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
39 import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
40 import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
41 import org.apache.hc.core5.http.HttpHost;
42 import org.apache.hc.core5.http.config.Registry;
43 import org.apache.hc.core5.http.config.RegistryBuilder;
44 import org.apache.hc.core5.http.io.SocketConfig;
45 import org.apache.hc.core5.http.io.entity.EntityUtils;
46 import org.apache.hc.core5.http.message.StatusLine;
47 import org.apache.hc.core5.http.protocol.HttpContext;
48 import org.apache.hc.core5.util.TimeValue;
49
50
51
52
53
54
55 public class ClientExecuteSOCKS {
56
57 public static void main(final String[] args)throws Exception {
58 final Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
59 .register("http", new MyConnectionSocketFactory())
60 .build();
61 final InetSocketAddress socksaddr = new InetSocketAddress("mysockshost", 1234);
62 final PoolingHttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
63 .setDefaultSocketConfig(SocketConfig.custom()
64 .setSocksProxyAddress(socksaddr)
65 .build())
66 .build();
67 try (final CloseableHttpClient httpclient = HttpClients.custom()
68 .setConnectionManager(cm)
69 .build()) {
70
71 final HttpHost target = new HttpHost("http", "httpbin.org", 80);
72 final HttpGet request = new HttpGet("/get");
73
74 System.out.println("Executing request " + request.getMethod() + " " + request.getUri() +
75 " via SOCKS proxy " + socksaddr);
76 httpclient.execute(target, request, response -> {
77 System.out.println("----------------------------------------");
78 System.out.println(request + "->" + new StatusLine(response));
79 EntityUtils.consume(response.getEntity());
80 return null;
81 });
82 }
83 }
84
85 static class MyConnectionSocketFactory implements ConnectionSocketFactory {
86
87 @Override
88 public Socket createSocket(final HttpContext context) throws IOException {
89 final InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address");
90 final Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
91 return new Socket(proxy);
92 }
93
94 @Override
95 public Socket connectSocket(
96 final TimeValue connectTimeout,
97 final Socket socket,
98 final HttpHost host,
99 final InetSocketAddress remoteAddress,
100 final InetSocketAddress localAddress,
101 final HttpContext context) throws IOException {
102 final Socket sock;
103 if (socket != null) {
104 sock = socket;
105 } else {
106 sock = createSocket(context);
107 }
108 if (localAddress != null) {
109 sock.bind(localAddress);
110 }
111 sock.connect(remoteAddress, connectTimeout != null ? connectTimeout.toMillisecondsIntBound() : 0);
112 return sock;
113 }
114
115 }
116
117 }