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.BufferedReader;
31  import java.io.InputStreamReader;
32  import java.io.OutputStreamWriter;
33  import java.io.Writer;
34  import java.net.Socket;
35  import java.nio.charset.StandardCharsets;
36  
37  import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
38  import org.apache.hc.client5.http.impl.classic.ProxyClient;
39  import org.apache.hc.core5.http.HttpHost;
40  
41  /**
42   * Example code for using {@link ProxyClient} in order to establish a tunnel through an HTTP proxy.
43   */
44  public class ProxyTunnelDemo {
45  
46      public final static void main(final String[] args) throws Exception {
47  
48          final ProxyClient proxyClient = new ProxyClient();
49          final HttpHost target = new HttpHost("www.yahoo.com", 80);
50          final HttpHost proxy = new HttpHost("localhost", 8888);
51          final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd".toCharArray());
52          try (final Socket socket = proxyClient.tunnel(proxy, target, credentials)) {
53              final Writer out = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.ISO_8859_1);
54              out.write("GET / HTTP/1.1\r\n");
55              out.write("Host: " + target.toHostString() + "\r\n");
56              out.write("Agent: whatever\r\n");
57              out.write("Connection: close\r\n");
58              out.write("\r\n");
59              out.flush();
60              final BufferedReader in = new BufferedReader(
61                      new InputStreamReader(socket.getInputStream(), StandardCharsets.ISO_8859_1));
62              String line = null;
63              while ((line = in.readLine()) != null) {
64                  System.out.println(line);
65              }
66          }
67      }
68  
69  }
70