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.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   * How to send a request via SOCKS proxy.
51   *
52   * @since 4.1
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 }