Search in sources :

Example 1 with ProxyClient

use of org.apache.hc.client5.http.impl.classic.ProxyClient in project mercury by yellow013.

the class ProxyTunnelDemo method main.

public static final 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("localhost", 8888);
    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);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ProxyClient(org.apache.hc.client5.http.impl.classic.ProxyClient) HttpHost(org.apache.hc.core5.http.HttpHost) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Aggregations

BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 Socket (java.net.Socket)1 UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)1 ProxyClient (org.apache.hc.client5.http.impl.classic.ProxyClient)1 HttpHost (org.apache.hc.core5.http.HttpHost)1