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