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

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

Introduction

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

Prototype

public ProxyClient() 

Source Link

Usage

From source file:ConnectMethodExampleForProxyClient.java

public static void main(String args[]) {

    ProxyClient client = new ProxyClient();
    client.getParams().setParameter("http.useragent", "Proxy Test Client");

    client.getHostConfiguration().setHost("www.somehost.com");
    client.getHostConfiguration().setProxy("localproxyaddress", 80);

    Socket socket = null;//from  w  w w . ja  va 2 s  .  c o m

    try {
        ConnectResponse response = client.connect();
        socket = response.getSocket();
        if (socket == null) {
            ConnectMethod method = response.getConnectMethod();
            System.err.println("Socket not created: " + method.getStatusLine());
        }
        // do something
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        if (socket != null)
            try {
                socket.close();
            } catch (Exception fe) {
            }
    }

}

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 w  w .j  ava2s  . 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 av a2 s  .co m
    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  w w w. j  a  va 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();
}

From source file:org.zaproxy.zap.extension.ascanrulesAlpha.InsecureHTTPMethod.java

private boolean testConnect(HttpMessage baseMsg, String thirdpartyHost, int thirdpartyPort,
        Pattern thirdPartyContentPattern) throws Exception {
    String connecthost = baseMsg.getRequestHeader().getURI().getHost();
    int connectport = baseMsg.getRequestHeader().getURI().getPort();

    //this cannot currently be done using the existing HttpSender class, so do it natively using HttpClient, 
    //in as simple as possible a manner.
    Socket socket = null;// w w  w  .j a v  a2  s .  co  m
    try {
        ProxyClient client = new ProxyClient();
        client.getHostConfiguration().setProxy(connecthost, connectport);
        client.getHostConfiguration().setHost(thirdpartyHost, thirdpartyPort);
        ConnectResponse connectResponse = client.connect();
        StatusLine statusLine = connectResponse.getConnectMethod().getStatusLine();

        if (log.isDebugEnabled())
            log.debug("The status line returned: " + statusLine);

        int statusCode = statusLine.getStatusCode();
        socket = connectResponse.getSocket();
        if (socket != null && statusCode == HttpStatus.SC_OK) {
            //we have a socket and a 200 status. 
            //Could still be a false positive though, if the server ignored the method, 
            //and did not recognise the URL, so redirected to a login page, for instance
            //Remediation: Check the contents match the expected third party contents.
            if (log.isDebugEnabled())
                log.debug("Raw Socket established, in theory to " + thirdpartyHost);

            OutputStream os = socket.getOutputStream();
            InputStream is = socket.getInputStream();

            PrintWriter pw = new PrintWriter(os, false);
            pw.write("GET http://" + thirdpartyHost + ":" + thirdpartyPort + "/ HTTP/1.1\n");
            pw.write("Host: " + thirdpartyHost + "\n\n");
            pw.flush();

            //read the response via a 4k buffer
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024 * 4];
            int bytesRead = is.read(buffer);
            int totalBytesRead = 0;
            while (bytesRead > -1) {
                totalBytesRead += bytesRead;
                bos.write(buffer, 0, bytesRead);
                bytesRead = is.read(buffer);
            }
            String response = new String(bos.toByteArray());
            if (log.isDebugEnabled())
                log.debug("Response is " + totalBytesRead + " bytes: \n" + response);

            Matcher m = thirdPartyContentPattern.matcher(response);
            if (m.matches()) {
                if (log.isDebugEnabled())
                    log.debug("Response matches expected third party pattern!");
                is.close();
                os.close();
                bos.close();
                socket.close();
                return true;
            } else {
                if (log.isDebugEnabled())
                    log.debug("Response does *not* match expected third party pattern");
            }
            is.close();
            os.close();
            bos.close();
            socket.close();
            return false;
        } else {
            if (log.isDebugEnabled()) {
                log.debug(
                        "Could not establish a socket connection to a third party using the CONNECT HTTP method: NULL socket returned, or non-200 response");
                log.debug("The status line returned: " + statusLine);
            }
            return false;
        }
    } catch (Exception e) {
        if (log.isDebugEnabled())
            log.debug("Could not establish a socket connection to a third party using the CONNECT HTTP method",
                    e);
    }
    return false;
}

From source file:org.zaproxy.zap.extension.ascanrulesBeta.InsecureHTTPMethod.java

private void testConnect(HttpMessage baseMsg, String thirdpartyHost, int thirdpartyPort,
        Pattern thirdPartyContentPattern) throws Exception {

    String connecthost = baseMsg.getRequestHeader().getURI().getHost();
    int connectport = baseMsg.getRequestHeader().getURI().getPort();

    // this cannot currently be done using the existing HttpSender class, so
    // do it natively using HttpClient,
    // in as simple as possible a manner.
    Socket socket = null;/*w w w .ja  v a  2s .  c  o m*/
    OutputStream os = null;
    InputStream is = null;
    ByteArrayOutputStream bos = null;
    try {
        ProxyClient client = new ProxyClient();
        client.getHostConfiguration().setProxy(connecthost, connectport);
        client.getHostConfiguration().setHost(thirdpartyHost, thirdpartyPort);
        ConnectResponse connectResponse = client.connect();

        StatusLine statusLine = connectResponse.getConnectMethod().getStatusLine();

        if (log.isDebugEnabled())
            log.debug("The status line returned: " + statusLine);

        int statusCode = statusLine.getStatusCode();

        socket = connectResponse.getSocket();
        if (socket != null && statusCode == HttpStatus.SC_OK) {
            // we have a socket and a 200 status.
            // Could still be a false positive though, if the server ignored
            // the method,
            // and did not recognise the URL, so redirected to a login page,
            // for instance
            // Remediation: Check the contents match the expected third
            // party contents.
            if (log.isDebugEnabled())
                log.debug("Raw Socket established, in theory to " + thirdpartyHost);

            os = socket.getOutputStream();
            is = socket.getInputStream();

            PrintWriter pw = new PrintWriter(os, false);
            pw.write("GET http://" + thirdpartyHost + ":" + thirdpartyPort + "/ HTTP/1.1\n");
            pw.write("Host: " + thirdpartyHost + "\n\n");
            pw.flush();

            // read the response via a 4k buffer
            bos = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            int bytesRead = is.read(buffer);
            int totalBytesRead = 0;
            while (bytesRead > -1) {
                totalBytesRead += bytesRead;
                bos.write(buffer, 0, bytesRead);
                bytesRead = is.read(buffer);
            }
            String response = new String(bos.toByteArray());
            if (log.isDebugEnabled())
                log.debug("Response is " + totalBytesRead + " bytes: \n" + response);

            Matcher m = thirdPartyContentPattern.matcher(response);
            if (m.matches()) {
                log.debug("Response matches expected third party pattern!");
                bingo(Alert.RISK_MEDIUM, Alert.CONFIDENCE_MEDIUM,
                        Constant.messages.getString("ascanbeta.insecurehttpmethod.detailed.name",
                                HttpRequestHeader.CONNECT),
                        Constant.messages.getString("ascanbeta.insecurehttpmethod.connect.exploitable.desc",
                                HttpRequestHeader.CONNECT),
                        null, null, // parameter being attacked: none.
                        "", // attack
                        Constant.messages.getString(
                                "ascanbeta.insecurehttpmethod.connect.exploitable.extrainfo", thirdpartyHost),
                        Constant.messages.getString("ascanbeta.insecurehttpmethod.soln"), response, // evidence,
                        this.getBaseMsg());
                return;
            } else {
                log.debug("Response does *not* match expected third party pattern");
            }

        } else {
            if (log.isDebugEnabled()) {
                log.debug(
                        "Could not establish a socket connection to a third party using the CONNECT HTTP method: NULL socket returned, or non-200 response");
                log.debug("The status line returned: " + statusLine);
            }
        }
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("Could not establish a socket connection to a third party using the CONNECT HTTP method",
                    e);
        }
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(socket);
    }
}