View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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   * How to send a request via SOCKS proxy.
52   *
53   * @since 4.1
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 }