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.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
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