Example usage for org.apache.commons.httpclient HostConfiguration setLocalAddress

List of usage examples for org.apache.commons.httpclient HostConfiguration setLocalAddress

Introduction

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

Prototype

public void setLocalAddress(InetAddress paramInetAddress)

Source Link

Usage

From source file:com.exalead.io.failover.MonitoredHttpConnectionManager.java

/**
 * Gets the host configuration for a connection.
 * @param conn the connection to get the configuration of
 * @return a new HostConfiguration//www  .  j a v  a 2s . c  om
 */
static HostConfiguration rebuildConfigurationFromConnection(HttpConnection conn) {
    HostConfiguration connectionConfiguration = new HostConfiguration();
    connectionConfiguration.setHost(conn.getHost(), conn.getPort(), conn.getProtocol());
    if (conn.getLocalAddress() != null) {
        connectionConfiguration.setLocalAddress(conn.getLocalAddress());
    }
    if (conn.getProxyHost() != null) {
        connectionConfiguration.setProxy(conn.getProxyHost(), conn.getProxyPort());
    }
    return connectionConfiguration;
}

From source file:com.predic8.membrane.integration.AccessControlInterceptorIntegrationTest.java

private HttpClient getClient(byte[] ip) throws UnknownHostException {
    HttpClient client = new HttpClient();
    HostConfiguration config = new HostConfiguration();
    config.setLocalAddress(InetAddress.getByAddress(ip));
    client.setHostConfiguration(config);
    return client;
}

From source file:io.hops.hopsworks.api.admin.YarnUIProxyServlet.java

@Override
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {

    if (servletRequest.getUserPrincipal() == null) {
        servletResponse.sendError(403, "User is not logged in");
        return;//from w ww .  j av a2s  . c om
    }
    if (!servletRequest.isUserInRole("HOPS_ADMIN")) {
        servletResponse.sendError(Response.Status.BAD_REQUEST.getStatusCode(),
                "You don't have the access right for this service");
        return;
    }
    if (servletRequest.getAttribute(ATTR_TARGET_URI) == null) {
        servletRequest.setAttribute(ATTR_TARGET_URI, targetUri);
    }
    if (servletRequest.getAttribute(ATTR_TARGET_HOST) == null) {
        servletRequest.setAttribute(ATTR_TARGET_HOST, targetHost);
    }

    // Make the Request
    // note: we won't transfer the protocol version because I'm not 
    // sure it would truly be compatible
    String proxyRequestUri = rewriteUrlFromRequest(servletRequest);

    try {
        // Execute the request

        HttpClientParams params = new HttpClientParams();
        params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        HttpClient client = new HttpClient(params);
        HostConfiguration config = new HostConfiguration();
        InetAddress localAddress = InetAddress.getLocalHost();
        config.setLocalAddress(localAddress);

        String method = servletRequest.getMethod();
        HttpMethod m;
        if (method.equalsIgnoreCase("PUT")) {
            m = new PutMethod(proxyRequestUri);
            RequestEntity requestEntity = new InputStreamRequestEntity(servletRequest.getInputStream(),
                    servletRequest.getContentType());
            ((PutMethod) m).setRequestEntity(requestEntity);
        } else {
            m = new GetMethod(proxyRequestUri);
        }
        Enumeration<String> names = servletRequest.getHeaderNames();
        while (names.hasMoreElements()) {
            String headerName = names.nextElement();
            String value = servletRequest.getHeader(headerName);
            if (PASS_THROUGH_HEADERS.contains(headerName)) {
                //yarn does not send back the js if encoding is not accepted
                //but we don't want to accept encoding for the html because we
                //need to be able to parse it
                if (headerName.equalsIgnoreCase("accept-encoding") && (servletRequest.getPathInfo() == null
                        || !servletRequest.getPathInfo().contains(".js"))) {
                    continue;
                } else {
                    m.setRequestHeader(headerName, value);
                }
            }
        }
        String user = servletRequest.getRemoteUser();
        if (user != null && !user.isEmpty()) {
            m.setRequestHeader("Cookie", "proxy-user" + "=" + URLEncoder.encode(user, "ASCII"));
        }

        client.executeMethod(config, m);

        // Process the response
        int statusCode = m.getStatusCode();

        // Pass the response code. This method with the "reason phrase" is 
        //deprecated but it's the only way to pass the reason along too.
        //noinspection deprecation
        servletResponse.setStatus(statusCode, m.getStatusLine().getReasonPhrase());

        copyResponseHeaders(m, servletRequest, servletResponse);

        // Send the content to the client
        copyResponseEntity(m, servletResponse);

    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        if (e instanceof ServletException) {
            throw (ServletException) e;
        }
        //noinspection ConstantConditions
        if (e instanceof IOException) {
            throw (IOException) e;
        }
        throw new RuntimeException(e);

    }
}

From source file:io.hops.hopsworks.api.admin.HDFSUIProxyServlet.java

@Override
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {

    if (servletRequest.getUserPrincipal() == null) {
        servletResponse.sendError(403, "User is not logged in");
        return;//from w  ww.  j av  a2 s . co m
    }
    if (!servletRequest.isUserInRole("HOPS_ADMIN")) {
        servletResponse.sendError(Response.Status.BAD_REQUEST.getStatusCode(),
                "You don't have the access right for this service");
        return;
    }
    if (servletRequest.getAttribute(ATTR_TARGET_URI) == null) {
        servletRequest.setAttribute(ATTR_TARGET_URI, targetUri);
    }
    if (servletRequest.getAttribute(ATTR_TARGET_HOST) == null) {
        servletRequest.setAttribute(ATTR_TARGET_HOST, targetHost);
    }

    // Make the Request
    // note: we won't transfer the protocol version because I'm not 
    // sure it would truly be compatible
    String proxyRequestUri = rewriteUrlFromRequest(servletRequest);

    try {
        String[] targetHost_port = settings.getHDFSWebUIAddress().split(":");
        File keyStore = new File(baseHadoopClientsService.getSuperKeystorePath());
        File trustStore = new File(baseHadoopClientsService.getSuperTrustStorePath());
        // Assume that KeyStore password and Key password are the same
        Protocol httpsProto = new Protocol("https",
                new CustomSSLProtocolSocketFactory(keyStore,
                        baseHadoopClientsService.getSuperKeystorePassword(),
                        baseHadoopClientsService.getSuperKeystorePassword(), trustStore,
                        baseHadoopClientsService.getSuperTrustStorePassword()),
                Integer.parseInt(targetHost_port[1]));
        Protocol.registerProtocol("https", httpsProto);
        // Execute the request
        HttpClientParams params = new HttpClientParams();
        params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        HttpClient client = new HttpClient(params);
        HostConfiguration config = new HostConfiguration();
        InetAddress localAddress = InetAddress.getLocalHost();
        config.setLocalAddress(localAddress);

        HttpMethod m = new GetMethod(proxyRequestUri);
        Enumeration<String> names = servletRequest.getHeaderNames();
        while (names.hasMoreElements()) {
            String headerName = names.nextElement();
            String value = servletRequest.getHeader(headerName);
            if (PASS_THROUGH_HEADERS.contains(headerName)) {
                //hdfs does not send back the js if encoding is not accepted
                //but we don't want to accept encoding for the html because we
                //need to be able to parse it
                if (headerName.equalsIgnoreCase("accept-encoding") && (servletRequest.getPathInfo() == null
                        || !servletRequest.getPathInfo().contains(".js"))) {
                    continue;
                } else {
                    m.setRequestHeader(headerName, value);
                }
            }
        }
        String user = servletRequest.getRemoteUser();
        if (user != null && !user.isEmpty()) {
            m.setRequestHeader("Cookie", "proxy-user" + "=" + URLEncoder.encode(user, "ASCII"));
        }

        client.executeMethod(config, m);

        // Process the response
        int statusCode = m.getStatusCode();

        // Pass the response code. This method with the "reason phrase" is 
        //deprecated but it's the only way to pass the reason along too.
        //noinspection deprecation
        servletResponse.setStatus(statusCode, m.getStatusLine().getReasonPhrase());

        copyResponseHeaders(m, servletRequest, servletResponse);

        // Send the content to the client
        copyResponseEntity(m, servletResponse);

    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        if (e instanceof ServletException) {
            throw (ServletException) e;
        }
        //noinspection ConstantConditions
        if (e instanceof IOException) {
            throw (IOException) e;
        }
        throw new RuntimeException(e);

    }
}

From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java

/**
 * Setup local bind address, based on attributes in CrawlURI and settings, 
 * in the given HostConfiguration //from w  w  w.  j  a  v  a2 s.  c  o m
 */
private void configureBindAddress(CrawlURI curi, HostConfiguration config) {
    String addressString = (String) getAttributeEither(curi, ATTR_HTTP_BIND_ADDRESS);
    if (addressString != null && addressString.length() > 0) {
        try {
            InetAddress localAddress = InetAddress.getByName(addressString);
            config.setLocalAddress(localAddress);
        } catch (UnknownHostException e) {
            // Convert all to RuntimeException so get an exception out
            // if initialization fails.
            throw new RuntimeException("Unknown host " + addressString + " in " + ATTR_HTTP_BIND_ADDRESS);
        }
    }
}

From source file:flex.messaging.services.http.HTTPProxyAdapter.java

private void initHttpConnectionManagerParams(HTTPConnectionManagerSettings settings) {
    connectionParams = new HttpConnectionManagerParams();
    connectionParams.setMaxTotalConnections(settings.getMaxTotalConnections());
    connectionParams.setDefaultMaxConnectionsPerHost(settings.getDefaultMaxConnectionsPerHost());

    if (!settings.getCookiePolicy().equals(CookiePolicy.DEFAULT)) {
        HttpClientParams httpClientParams = (HttpClientParams) connectionParams.getDefaults();
        httpClientParams.setCookiePolicy(settings.getCookiePolicy());
    }/*from   ww  w.j  ava2  s  . c om*/

    if (settings.getConnectionTimeout() >= 0)
        connectionParams.setConnectionTimeout(settings.getConnectionTimeout());

    if (settings.getSocketTimeout() >= 0)
        connectionParams.setSoTimeout(settings.getSocketTimeout());

    connectionParams.setStaleCheckingEnabled(settings.isStaleCheckingEnabled());

    if (settings.getSendBufferSize() > 0)
        connectionParams.setSendBufferSize(settings.getSendBufferSize());

    if (settings.getReceiveBufferSize() > 0)
        connectionParams.setReceiveBufferSize(settings.getReceiveBufferSize());

    connectionParams.setTcpNoDelay(settings.isTcpNoDelay());
    connectionParams.setLinger(settings.getLinger());

    if (settings.getMaxConnectionsPerHost() != null) {
        Iterator it = settings.getMaxConnectionsPerHost().iterator();
        while (it.hasNext()) {
            HostConfigurationSettings hcs = (HostConfigurationSettings) it.next();
            HostConfiguration hostConfig = new HostConfiguration();

            if (hcs.getProtocol() != null) {
                Protocol protocol = Protocol.getProtocol(hcs.getProtocol());
                hostConfig.setHost(hcs.getHost(), hcs.getPort(), protocol);
            } else if (hcs.getProtocolFactory() != null) {
                Protocol protocol = hcs.getProtocolFactory().getProtocol();
                if (hcs.getPort() > 0)
                    hostConfig.setHost(hcs.getHost(), hcs.getPort(), protocol);
                else
                    hostConfig.setHost(hcs.getHost(), protocol.getDefaultPort(), protocol);
            } else {
                if (hcs.getPort() > 0)
                    hostConfig.setHost(hcs.getHost(), hcs.getPort());
                else
                    hostConfig.setHost(hcs.getHost());
            }

            if (hcs.getVirtualHost() != null) {
                HostParams params = hostConfig.getParams();
                if (params != null)
                    params.setVirtualHost(hcs.getVirtualHost());
            }

            if (hcs.getProxyHost() != null) {
                hostConfig.setProxy(hcs.getProxyHost(), hcs.getProxyPort());
            }

            try {
                InetAddress addr = InetAddress.getByName(hcs.getLocalAddress());
                hostConfig.setLocalAddress(addr);
            } catch (UnknownHostException ex) {
            }
            connectionParams.setMaxConnectionsPerHost(hostConfig, hcs.getMaximumConnections());
        }
    }
}

From source file:org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet.java

/**
 * Download link and have it be the response.
 * @param req the http request// ww w .  j  av  a 2  s. co m
 * @param resp the http response
 * @param link the link to download
 * @param c the cookie to set if any
 * @throws IOException on any error.
 */
private static void proxyLink(HttpServletRequest req, HttpServletResponse resp, URI link, Cookie c,
        String proxyHost) throws IOException {
    org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(link.toString(), false);
    HttpClientParams params = new HttpClientParams();
    params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
    HttpClient client = new HttpClient(params);
    // Make sure we send the request from the proxy address in the config
    // since that is what the AM filter checks against. IP aliasing or
    // similar could cause issues otherwise.
    HostConfiguration config = new HostConfiguration();
    InetAddress localAddress = InetAddress.getByName(proxyHost);
    if (LOG.isDebugEnabled()) {
        LOG.debug("local InetAddress for proxy host: " + localAddress.toString());
    }
    config.setLocalAddress(localAddress);
    HttpMethod method = new GetMethod(uri.getEscapedURI());
    @SuppressWarnings("unchecked")
    Enumeration<String> names = req.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = names.nextElement();
        if (passThroughHeaders.contains(name)) {
            String value = req.getHeader(name);
            LOG.debug("REQ HEADER: " + name + " : " + value);
            method.setRequestHeader(name, value);
        }
    }

    String user = req.getRemoteUser();
    if (user != null && !user.isEmpty()) {
        method.setRequestHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII"));
    }
    OutputStream out = resp.getOutputStream();
    try {
        resp.setStatus(client.executeMethod(config, method));
        for (Header header : method.getResponseHeaders()) {
            resp.setHeader(header.getName(), header.getValue());
        }
        if (c != null) {
            resp.addCookie(c);
        }
        InputStream in = method.getResponseBodyAsStream();
        if (in != null) {
            IOUtils.copyBytes(in, out, 4096, true);
        }
    } finally {
        method.releaseConnection();
    }
}

From source file:org.apache.jmeter.protocol.http.sampler.HTTPHC3Impl.java

/**
 * Returns an <code>HttpConnection</code> fully ready to attempt
 * connection. This means it sets the request method (GET or POST), headers,
 * cookies, and authorization for the URL request.
 * <p>/*from  w  ww.j ava2  s  .c  o m*/
 * The request infos are saved into the sample result if one is provided.
 *
 * @param u
 *            <code>URL</code> of the URL request
 * @param httpMethod
 *            GET/PUT/HEAD etc
 * @param res
 *            sample result to save request infos to
 * @return <code>HttpConnection</code> ready for .connect
 * @exception IOException
 *                if an I/O Exception occurs
 */
protected HttpClient setupConnection(URL u, HttpMethodBase httpMethod, HTTPSampleResult res)
        throws IOException {

    String urlStr = u.toString();

    org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(urlStr, false);

    String schema = uri.getScheme();
    if ((schema == null) || (schema.length() == 0)) {
        schema = HTTPConstants.PROTOCOL_HTTP;
    }

    final boolean isHTTPS = HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(schema);
    if (isHTTPS) {
        SSLManager.getInstance(); // ensure the manager is initialised
        // we don't currently need to do anything further, as this sets the default https protocol
    }

    Protocol protocol = Protocol.getProtocol(schema);

    String host = uri.getHost();
    int port = uri.getPort();

    /*
     *  We use the HostConfiguration as the key to retrieve the HttpClient,
     *  so need to ensure that any items used in its equals/hashcode methods are
     *  not changed after use, i.e.:
     *  host, port, protocol, localAddress, proxy
     *
    */
    HostConfiguration hc = new HostConfiguration();
    hc.setHost(host, port, protocol); // All needed to ensure re-usablility

    // Set up the local address if one exists
    final InetAddress inetAddr = getIpSourceAddress();
    if (inetAddr != null) {// Use special field ip source address (for pseudo 'ip spoofing')
        hc.setLocalAddress(inetAddr);
    } else {
        hc.setLocalAddress(localAddress); // null means use the default
    }

    final String proxyHost = getProxyHost();
    final int proxyPort = getProxyPortInt();

    boolean useStaticProxy = isStaticProxy(host);
    boolean useDynamicProxy = isDynamicProxy(proxyHost, proxyPort);

    if (useDynamicProxy) {
        hc.setProxy(proxyHost, proxyPort);
        useStaticProxy = false; // Dynamic proxy overrules static proxy
    } else if (useStaticProxy) {
        if (log.isDebugEnabled()) {
            log.debug("Setting proxy: " + PROXY_HOST + ":" + PROXY_PORT);
        }
        hc.setProxy(PROXY_HOST, PROXY_PORT);
    }

    Map<HostConfiguration, HttpClient> map = httpClients.get();
    // N.B. HostConfiguration.equals() includes proxy settings in the compare.
    HttpClient httpClient = map.get(hc);

    if (httpClient != null && resetSSLContext && isHTTPS) {
        httpClient.getHttpConnectionManager().closeIdleConnections(-1000);
        httpClient = null;
        JsseSSLManager sslMgr = (JsseSSLManager) SSLManager.getInstance();
        sslMgr.resetContext();
        resetSSLContext = false;
    }

    if (httpClient == null) {
        httpClient = new HttpClient(new SimpleHttpConnectionManager());
        httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(RETRY_COUNT, false));
        if (log.isDebugEnabled()) {
            log.debug("Created new HttpClient: @" + System.identityHashCode(httpClient));
        }
        httpClient.setHostConfiguration(hc);
        map.put(hc, httpClient);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Reusing the HttpClient: @" + System.identityHashCode(httpClient));
        }
    }

    // Set up any required Proxy credentials
    if (useDynamicProxy) {
        String user = getProxyUser();
        if (user.length() > 0) {
            httpClient.getState().setProxyCredentials(
                    new AuthScope(proxyHost, proxyPort, null, AuthScope.ANY_SCHEME),
                    new NTCredentials(user, getProxyPass(), localHost, PROXY_DOMAIN));
        } else {
            httpClient.getState().clearProxyCredentials();
        }
    } else {
        if (useStaticProxy) {
            if (PROXY_USER.length() > 0) {
                httpClient.getState().setProxyCredentials(
                        new AuthScope(PROXY_HOST, PROXY_PORT, null, AuthScope.ANY_SCHEME),
                        new NTCredentials(PROXY_USER, PROXY_PASS, localHost, PROXY_DOMAIN));
            }
        } else {
            httpClient.getState().clearProxyCredentials();
        }
    }

    int rto = getResponseTimeout();
    if (rto > 0) {
        httpMethod.getParams().setSoTimeout(rto);
    }

    int cto = getConnectTimeout();
    if (cto > 0) {
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(cto);
    }

    // Allow HttpClient to handle the redirects:
    httpMethod.setFollowRedirects(getAutoRedirects());

    // a well-behaved browser is supposed to send 'Connection: close'
    // with the last request to an HTTP server. Instead, most browsers
    // leave it to the server to close the connection after their
    // timeout period. Leave it to the JMeter user to decide.
    if (getUseKeepAlive()) {
        httpMethod.setRequestHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.KEEP_ALIVE);
    } else {
        httpMethod.setRequestHeader(HTTPConstants.HEADER_CONNECTION, HTTPConstants.CONNECTION_CLOSE);
    }

    setConnectionHeaders(httpMethod, u, getHeaderManager(), getCacheManager());
    String cookies = setConnectionCookie(httpMethod, u, getCookieManager());

    setConnectionAuthorization(httpClient, u, getAuthManager());

    if (res != null) {
        res.setCookies(cookies);
    }

    return httpClient;
}

From source file:org.lockss.plugin.silverchair.PostHttpClientUrlConnection.java

/** Execute the request. */
public void execute() throws IOException {
    assertNotExecuted();/*from  w w  w.j a va2s. co  m*/
    if (methodCode != LockssUrlConnection.METHOD_PROXY) {
        mimicSunRequestHeaders();
    }
    HostConfiguration hostConfig = client.getHostConfiguration();
    if (proxyHost != null) {
        hostConfig.setProxy(proxyHost, proxyPort);
    } else {
        hostConfig.setProxyHost(null);
    }
    if (localAddress != null) {
        hostConfig.setLocalAddress(localAddress.getInetAddr());
    } else {
        hostConfig.setLocalAddress(null);
    }
    if (sockFact != null) {
        SecureProtocolSocketFactory hcSockFact = sockFact.getHttpClientSecureProtocolSocketFactory();
        String host = url.getHost();
        int port = url.getPort();
        if (port <= 0) {
            port = UrlUtil.getDefaultPort(url.getProtocol().toLowerCase());
        }
        DISP_FACT.setFactory(host, port, hcSockFact);
        // XXX Would like to check after connection is made that cert check
        // was actually done, but there's no good way to get to the socket or
        // SSLContect, etc.
        isAuthenticatedServer = sockFact.requiresServerAuth();
    }
    isExecuted = true;
    responseCode = executeOnce(method);
}

From source file:org.opentides.util.UrlUtil.java

/**
 * Returns the HTML code of the original engine. Takes the URL to connect to
 * the engine. Also takes encoding type that overrides default if not null
 * "UTF8" is typical encoding type/*from  w  w w  .  j  a  v  a2  s . c  om*/
 * 
 * @param queryURL
 *            - URL of engine to retrieve
 * @param request
 *            - request object
 * @param param
 *            - additional parameters
 *              - Valid parameters are:
 *              - methodName - Either "POST" or "GET". Default is "POST"   
 *            - forwardCookie - if true, will forward cookies found on request object
 *            - IPAddress - if specified, this IP will be used for the request
 *        
 */
public static final UrlResponseObject getPage(final String queryURL, final HttpServletRequest request,
        final Map<String, Object> param) {
    // determine if get or post method
    HttpMethodBase httpMethodBase;
    Boolean forwardCookie = false;
    InetAddress IPAddress = null;

    if (param != null) {
        if (param.get("forwardCookie") != null)
            forwardCookie = (Boolean) param.get("forwardCookie");

        if (param.get("IPAddress") != null) {
            String IPString = (String) param.get("IPAddress");
            if (!StringUtil.isEmpty(IPString)) {
                IPAddress = convertIPString(IPString);
            }
        }
    }

    if (param != null && "GET".equals((String) param.get("methodName"))) {
        httpMethodBase = new GetMethod(queryURL);
    } else {
        httpMethodBase = new PostMethod(queryURL);
    }

    try {
        // declare the connection objects
        HttpClient client = new HttpClient();
        HostConfiguration hostConfig = new HostConfiguration();
        String userAgent = request.getHeader("User-Agent");

        // for debugging
        if (_log.isDebugEnabled())
            _log.debug("Retrieving page from " + queryURL);

        // initialize the connection settings
        client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);
        client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        httpMethodBase.addRequestHeader("accept", "*/*");
        httpMethodBase.addRequestHeader("accept-language", "en-us");
        httpMethodBase.addRequestHeader("user-agent", userAgent);

        if (forwardCookie) {
            // get cookies from request
            Cookie[] cookies = request.getCookies();
            String cookieString = "";
            for (Cookie c : cookies) {
                cookieString += c.getName() + "=" + c.getValue() + "; ";
            }

            // forward cookies to httpMethod
            httpMethodBase.setRequestHeader("Cookie", cookieString);
        }

        if (IPAddress != null) {
            hostConfig.setLocalAddress(IPAddress);
        }

        // Setup for 3 retry  
        httpMethodBase.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        // now let's retrieve the data
        client.executeMethod(hostConfig, httpMethodBase);

        // Read the response body.
        UrlResponseObject response = new UrlResponseObject();
        response.setResponseBody(httpMethodBase.getResponseBody());
        Header contentType = httpMethodBase.getResponseHeader("Content-Type");
        if (contentType != null)
            response.setResponseType(contentType.getValue());
        else
            response.setResponseType(Widget.TYPE_HTML);
        return response;

    } catch (Exception ex) {
        _log.error("Failed to request from URL: [" + queryURL + "]", ex);
        return null;
    } finally {
        try {
            httpMethodBase.releaseConnection();
        } catch (Exception ignored) {
        }
    }
}