Example usage for org.apache.commons.httpclient HttpConnection setProxyPort

List of usage examples for org.apache.commons.httpclient HttpConnection setProxyPort

Introduction

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

Prototype

public void setProxyPort(int port) throws IllegalStateException 

Source Link

Document

Sets the port of the host to proxy through.

Usage

From source file:it.intecs.pisa.toolbox.util.URLReader.java

public static Object getURLContent(String host, String url, int port) throws Exception {
    HttpMethod method;/*from  ww w .  j  a v a 2s.  co  m*/
    try {
        method = new GetMethod(url);
        HttpConnection conn = new HttpConnection(host, port);
        String proxyHost;
        String proxyPort;
        if ((proxyHost = System.getProperty("http.proxyHost")) != null
                && (proxyPort = System.getProperty("http.proxyPort")) != null) {
            conn.setProxyHost(proxyHost);
            conn.setProxyPort(Integer.parseInt(proxyPort));
        }
        conn.open();
        method.execute(new HttpState(), conn);
        String inpTmp = method.getResponseBodyAsString();
        Reader in = new InputStreamReader(method.getResponseBodyAsStream());
        StringBuffer out = new StringBuffer();
        char[] buffer = new char[1024];
        for (int count = in.read(buffer); count >= 0; count = in.read(buffer)) {
            out.append(buffer, 0, count);
        }
        return out.toString();
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
    }
}

From source file:es.gva.cit.catalog.DiscoveryServiceClient.java

/**
 * It tries if the server is ready//from w  w  w  .  j  a v a  2  s  . c o m
 * 
 * @return boolean true --> server is ready false --> server is not ready
 */
public boolean serverIsReady() throws ServerIsNotReadyException {
    Properties systemSettings = System.getProperties();

    Object isProxyEnabled = systemSettings.get("http.proxySet");
    if ((isProxyEnabled == null) || (isProxyEnabled.equals("false"))) {
        Socket sock;
        try {
            sock = new Socket(getUri().getHost(), getUri().getPort());
        } catch (UnknownHostException e) {
            throw new ServerIsNotReadyException(e);
        } catch (IOException e) {
            throw new ServerIsNotReadyException(e);
        }
        return (sock != null);
    } else {
        Object host = systemSettings.get("http.proxyHost");
        Object port = systemSettings.get("http.proxyPort");
        Object user = systemSettings.get("http.proxyUserName");
        Object password = systemSettings.get("http.proxyPassword");
        if ((host != null) && (port != null)) {
            int iPort = 80;
            try {
                iPort = Integer.parseInt((String) port);
            } catch (Exception e) {
                // Use 80
            }
            HttpConnection connection = new HttpConnection(getUri().getHost(), getUri().getPort());
            connection.setProxyHost((String) host);
            connection.setProxyPort(iPort);
            Authenticator.setDefault(new SimpleAuthenticator(user, password));

            try {
                connection.open();
                connection.close();
            } catch (IOException e) {
                throw new ServerIsNotReadyException(e);
            }
        }
    }
    return true;
}

From source file:com.cyberway.issue.httpclient.ThreadLocalHttpConnectionManager.java

/**
 * @see HttpConnectionManager#getConnectionWithTimeout(HostConfiguration, long)
 * /*w ww .  ja  v a2  s  . c  o m*/
 * @since 3.0
 */
public HttpConnection getConnectionWithTimeout(final HostConfiguration hostConfiguration, final long timeout) {

    final ConnectionInfo ci = getConnectionInfo();
    HttpConnection httpConnection = ci.conn;

    // make sure the host and proxy are correct for this connection
    // close it and set the values if they are not
    if (httpConnection == null || !finishLastResponse(httpConnection)
            || !hostConfiguration.hostEquals(httpConnection)
            || !hostConfiguration.proxyEquals(httpConnection)) {

        if (httpConnection != null && httpConnection.isOpen()) {
            closer.closeConnection(httpConnection);
        }

        httpConnection = new HttpConnection(hostConfiguration);
        httpConnection.setHttpConnectionManager(this);
        httpConnection.getParams().setDefaults(this.params);
        ci.conn = httpConnection;

        httpConnection.setHost(hostConfiguration.getHost());
        httpConnection.setPort(hostConfiguration.getPort());
        httpConnection.setProtocol(hostConfiguration.getProtocol());
        httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());

        httpConnection.setProxyHost(hostConfiguration.getProxyHost());
        httpConnection.setProxyPort(hostConfiguration.getProxyPort());
    }

    // remove the connection from the timeout handler
    ci.idleStartTime = Long.MAX_VALUE;

    return httpConnection;
}