-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathHttpClient5ProxyTunnelDemo.java
42 lines (36 loc) · 1.66 KB
/
HttpClient5ProxyTunnelDemo.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.wdbyte.httpclient;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.classic.ProxyClient;
import org.apache.hc.core5.http.HttpHost;
/**
* Example code for using {@link ProxyClient} in order to establish a tunnel through an HTTP proxy.
*/
public class HttpClient5ProxyTunnelDemo {
public final static void main(final String[] args) throws Exception {
final ProxyClient proxyClient = new ProxyClient();
final HttpHost target = new HttpHost("www.yahoo.com", 80);
final HttpHost proxy = new HttpHost("127.0.0.1", 9090);
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd".toCharArray());
try (final Socket socket = proxyClient.tunnel(proxy, target, credentials)) {
final Writer out = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.ISO_8859_1);
out.write("GET / HTTP/1.1\r\n");
out.write("Host: " + target.toHostString() + "\r\n");
out.write("Agent: whatever\r\n");
out.write("Connection: close\r\n");
out.write("\r\n");
out.flush();
final BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream(), StandardCharsets.ISO_8859_1));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
}
}