Example usage for org.apache.commons.httpclient ProxyClient getState

List of usage examples for org.apache.commons.httpclient ProxyClient getState

Introduction

In this page you can find the example usage for org.apache.commons.httpclient ProxyClient getState.

Prototype

public HttpState getState()

Source Link

Usage

From source file:ProxyTunnelDemo.java

public static void main(String[] args) throws Exception {

    ProxyClient proxyclient = new ProxyClient();
    // set the host the proxy should create a connection to
    ///*from w  ww. j  ava  2  s . c o m*/
    // Note:  By default port 80 will be used. Some proxies only allow conections
    // to ports 443 and 8443.  This is because the HTTP CONNECT method was intented
    // to be used for tunneling HTTPS.
    proxyclient.getHostConfiguration().setHost("www.yahoo.com");
    // set the proxy host and port
    proxyclient.getHostConfiguration().setProxy("10.0.1.1", 3128);
    // set the proxy credentials, only necessary for authenticating proxies
    proxyclient.getState().setProxyCredentials(new AuthScope("10.0.1.1", 3128, null),
            new UsernamePasswordCredentials("proxy", "proxy"));

    // create the socket
    ProxyClient.ConnectResponse response = proxyclient.connect();

    if (response.getSocket() != null) {
        Socket socket = response.getSocket();
        try {
            // go ahead and do an HTTP GET using the socket
            Writer out = new OutputStreamWriter(socket.getOutputStream(), "ISO-8859-1");
            out.write("GET http://www.yahoo.com/ HTTP/1.1\r\n");
            out.write("Host: www.yahoo.com\r\n");
            out.write("Agent: whatever\r\n");
            out.write("\r\n");
            out.flush();
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(socket.getInputStream(), "ISO-8859-1"));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } finally {
            // be sure to close the socket when we're done
            socket.close();
        }
    } else {
        // the proxy connect was not successful, check connect method for reasons why
        System.out.println("Connect failed: " + response.getConnectMethod().getStatusLine());
        System.out.println(response.getConnectMethod().getResponseBodyAsString());
    }
}

From source file:com.googlecode.gmail4j.http.HttpProxyAwareSslSocketFactory.java

@Override
public Socket createSocket() throws IOException {
    //FIXME won't work..
    log.debug("Creating socket! with proxy: " + proxy.address());
    InetSocketAddress addr = (InetSocketAddress) proxy.address();
    ProxyClient proxyClient = new ProxyClient();
    proxyClient.getHostConfiguration().setHost("imap.gmail.com", 993);
    proxyClient.getHostConfiguration().setProxy(addr.getHostName(), addr.getPort());
    if (proxyCredentials != null) {
        proxyClient.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
                proxyCredentials.getUsername(), new String(proxyCredentials.getPasword())));
    }//from   w w  w . j  ava2s  . c om
    log.debug("Trying to connect to proxy");
    ProxyClient.ConnectResponse resp = proxyClient.connect();
    if (resp.getConnectMethod().getStatusCode() != HttpStatus.SC_OK) {
        log.error("Failed to connect. " + resp.getConnectMethod().getStatusLine());
        throw new GmailException(
                "Failed connecting to IMAP through proxy: " + resp.getConnectMethod().getStatusLine());
    }
    log.debug("Connected, returning socket");
    return resp.getSocket();
}

From source file:org.ulteo.utils.ProxyManager.java

public Socket connect(String remoteHost, int remotePort) throws IOException, RdesktopException {
    ProxyClient proxyClient = new ProxyClient();

    Logger.debug("Connecting to proxy[" + this.host + ":" + this.port + "]");

    proxyClient.getHostConfiguration().setProxy(this.host, this.port);
    proxyClient.getHostConfiguration().setHost(remoteHost, remotePort);

    if (this.login != null && this.password != null) {
        Logger.debug("Use credentials");
        proxyClient.getState().setProxyCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(this.login, this.password));
    }//from   ww w.  j  ava 2 s. co m
    Logger.debug("Connected to proxy[" + this.host + ":" + this.port + "]");
    ProxyClient.ConnectResponse resp = proxyClient.connect();

    if (resp.getConnectMethod().getStatusCode() != HttpStatus.SC_OK) {
        Logger.debug("Failed to connect to proxy[" + this.host + ":" + this.port + "]"
                + resp.getConnectMethod().getStatusLine());
        return null;
    }
    Logger.debug("Proxy OK");
    return resp.getSocket();
}