Example usage for org.apache.commons.httpclient HttpMethodBase setRequestHeader

List of usage examples for org.apache.commons.httpclient HttpMethodBase setRequestHeader

Introduction

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

Prototype

@Override
public void setRequestHeader(String headerName, String headerValue) 

Source Link

Document

Set the specified request header, overwriting any previous value.

Usage

From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.java

public <T> T execute(Request<T> request, boolean authenticateIfNeeded, IProgressMonitor monitor)
        throws IOException, GerritException {
    String openIdProvider = getOpenIdProvider();

    hostConfiguration = WebUtil.createHostConfiguration(httpClient, location, monitor);

    for (int attempt = 0; attempt < 2; attempt++) {
        if (authenticateIfNeeded) {
            // force authentication
            if (needsAuthentication()) {
                AuthenticationCredentials credentials = location.getCredentials(AuthenticationType.REPOSITORY);
                if (openIdProvider != null || credentials != null) {
                    authenticate(openIdProvider, monitor);
                }//  w w w. j ava  2 s.  c  o m
            }
            if (!obtainedXsrfKey) {
                updateXsrfKey(monitor);
            }
        }

        HttpMethodBase method = request.createMethod();
        if (obtainedXsrfKey) {
            // required to authenticate against Gerrit 2.6+ REST endpoints
            // harmless in previous versions
            method.setRequestHeader(X_GERRIT_AUTHORITY, xsrfKey);
        }
        try {
            // Execute the method.
            WebUtil.execute(httpClient, hostConfiguration, method, monitor);
        } catch (IOException e) {
            WebUtil.releaseConnection(method, monitor);
            throw e;
        } catch (RuntimeException e) {
            WebUtil.releaseConnection(method, monitor);
            throw e;
        }

        int code = method.getStatusCode();
        if (code == HttpURLConnection.HTTP_OK || code == HttpURLConnection.HTTP_ACCEPTED
                || code == HttpURLConnection.HTTP_CREATED) {
            try {
                return request.process(method);
            } finally {
                WebUtil.releaseConnection(method, monitor);
            }
        } else if (code == HttpURLConnection.HTTP_NO_CONTENT) {
            try {
                return null;
            } finally {
                WebUtil.releaseConnection(method, monitor);
            }

        } else {
            try {
                request.handleError(method);
            } finally {
                WebUtil.releaseConnection(method, monitor);
            }
            if (code == HttpURLConnection.HTTP_UNAUTHORIZED || code == HttpURLConnection.HTTP_FORBIDDEN) {
                // login or re-authenticate due to an expired session
                authenticate(openIdProvider, monitor);
            } else {
                throw new GerritHttpException(code);
            }
        }
    }

    throw new GerritLoginException();
}

From source file:org.eclipse.php.composer.core.HttpHelper.java

public static HttpMethodBase setCookies(HttpMethodBase method, Map<String, String> params) {
    if (params != null) {
        StringBuilder builder = new StringBuilder();
        Set<String> keyList = params.keySet();
        for (String key : keyList) {
            builder.append(key);/*  w w w. j a v  a  2  s .co m*/
            builder.append("="); //$NON-NLS-1$
            builder.append(params.get(key));
            builder.append(";"); //$NON-NLS-1$
        }
        String value = builder.toString();
        if (value.length() > 0) {
            value = value.substring(0, value.length() - 1);
            method.setRequestHeader("Cookie", value); //$NON-NLS-1$
        }
    }
    return method;
}

From source file:org.eclipse.smila.connectivity.framework.crawler.web.http.HttpResponse.java

/**
 * Sets the http parameters./*from   w  w w.  j  a v a2 s  .  c  o m*/
 * 
 * @param http
 *          the http
 * @param httpMethod
 *          the http method
 */
private void setHttpParameters(HttpBase http, HttpMethodBase httpMethod) {
    httpMethod.setFollowRedirects(false);
    httpMethod.setRequestHeader("User-Agent", http.getUserAgent());
    httpMethod.setRequestHeader("Referer", http.getReferer());

    httpMethod.setDoAuthentication(true);

    for (Header header : http.getHeaders()) {
        httpMethod.addRequestHeader(header);
    }

    final HttpMethodParams params = httpMethod.getParams();
    if (http.getUseHttp11()) {
        params.setVersion(HttpVersion.HTTP_1_1);
    } else {
        params.setVersion(HttpVersion.HTTP_1_0);
    }
    params.makeLenient();
    params.setContentCharset("UTF-8");

    if (http.isCookiesEnabled()) {
        params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    } else {
        params.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    }
    params.setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
    // the default is to retry 3 times; if
    // the request body was sent the method is not retried, so there is
    // little danger in retrying
    // retries are handled on the higher level
    params.setParameter(HttpMethodParams.RETRY_HANDLER, null);
}

From source file:org.eclipse.winery.highlevelrestapi.HighLevelRestApi.java

private static void setAcceptHeader(HttpMethodBase method, String value) {
    if (!value.equals("")) {
        method.setRequestHeader("Accept", value);
    } else {// ww  w.  j  av  a 2s .c om
        method.setRequestHeader("Accept", "application/xml");
    }
}

From source file:org.exist.netedit.NetEditApplet.java

private void useCurrentSession(HttpMethodBase method) {
    if (sessionid != null) {
        method.setRequestHeader("Cookie", "JSESSIONID=" + sessionid);
    }/*  w w  w . j  a  v  a 2 s .c  o  m*/
}

From source file:org.jboss.mod_cluster.Client.java

public int runit() throws Exception {

    PostMethod pm = null;/*from  w w w  .jav a2  s. com*/
    GetMethod gm = null;
    HttpMethodBase bm = null;
    long starttime, endtime;
    if (httpClient == null)
        httpClient = new HttpClient();
    if (fd != null) {
        pm = new PostMethod(URL);
        // InputStreamRequestEntity buf = new InputStreamRequestEntity(fd);
        // XXX: Ugly hack to test...
        byte[] buffet = new byte[6144];
        for (int i = 0; i < buffet.length; i++)
            buffet[i] = 'a';
        ByteArrayRequestEntity buf = new ByteArrayRequestEntity(buffet);
        pm.setRequestEntity(buf);
        // pm.setRequestBody(fd);
        pm.setHttp11(true);
        pm.setContentChunked(true);
        // pm.setRequestContentLength(PostMethod.CONTENT_LENGTH_CHUNKED);
        bm = pm;
    } else if (post != null) {
        pm = new PostMethod(URL);
        pm.setRequestEntity(new StringRequestEntity(post, "application/x-www-form-urlencoded", "UTF8"));
        bm = pm;
    } else {
        gm = new GetMethod(URL);
        bm = gm;
    }
    if (user != null) {
        Credentials cred = new UsernamePasswordCredentials(user, pass);
        httpClient.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY, cred);
    }

    // System.out.println("Connecting to " + URL);

    Integer connectionTimeout = 40000;
    bm.getParams().setParameter("http.socket.timeout", connectionTimeout);
    bm.getParams().setParameter("http.connection.timeout", connectionTimeout);
    if (VirtualHost != null)
        bm.getParams().setVirtualHost(VirtualHost);
    httpClient.getParams().setParameter("http.socket.timeout", connectionTimeout);
    httpClient.getParams().setParameter("http.connection.timeout", connectionTimeout);
    if (jsessionid != null) {
        // System.out.println("jsessionid: " + jsessionid);
        bm.setRequestHeader("Cookie", "JSESSIONID=" + jsessionid);
    }

    try {
        if (gm == null) {
            pm.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
            starttime = System.currentTimeMillis();
            httpResponseCode = httpClient.executeMethod(pm);
            endtime = System.currentTimeMillis();
        } else {
            gm.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
            starttime = System.currentTimeMillis();
            httpResponseCode = httpClient.executeMethod(gm);
            endtime = System.currentTimeMillis();
        }

        if (httpResponseCode == 200) {
            response = bm.getResponseBodyAsString();
            Cookie[] cookies = httpClient.getState().getCookies();
            // System.out.println( "Cookies: " + cookies);
            if (cookies != null && cookies.length != 0) {
                for (int i = 0; i < cookies.length; i++) {
                    Cookie cookie = cookies[i];
                    // System.out.println( "Cookie: " + cookie.getName() + ", Value: " + cookie.getValue());
                    if (cookie.getName().equals("JSESSIONID")) {
                        if (jsessionid == null) {
                            jsessionid = cookie.getValue();
                            String nodes[] = jsessionid.split("\\.");
                            if (nodes.length == 2)
                                node = nodes[1];
                            System.out.println("cookie first time: " + jsessionid);
                            bm.releaseConnection();
                            return 0; // first time ok.
                        } else {
                            if (jsessionid.compareTo(cookie.getValue()) == 0) {
                                if (logok)
                                    if (bm.getResponseHeader("Date") != null)
                                        System.out.println("cookie ok: "
                                                + bm.getResponseHeader("Date").toString().replace('\r', ' ')
                                                        .replace('\n', ' ')
                                                + " response time: " + (endtime - starttime));
                                    else {
                                        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                                        Date date = new Date();
                                        System.out.println("cookie ok: " + dateFormat.format(date)
                                                + " response time: " + (endtime - starttime));
                                    }
                                bm.releaseConnection();
                                return 0;
                            } else {
                                System.out.println(
                                        "cookie \"second\" time: " + cookie.getValue() + " : " + jsessionid);
                                System.out.println("cookie changed");
                                bm.releaseConnection();
                                if (checkcookie)
                                    return -1;
                                else if (checknode) {
                                    String nodes[] = cookie.getValue().split("\\.");
                                    if (nodes.length != 2) {
                                        System.out.println("Can't find node in cookie");
                                        return -1;
                                    }
                                    if (nodes[1].compareTo(node) == 0) {
                                        return 0;
                                    } else {
                                        System.out.println("node " + nodes[1] + " changed too");
                                        return -1;
                                    }
                                } else
                                    return 0;
                            }
                        }
                    }
                }
            } else {
                // Look in the response to make sure that there is a cookie.
                int len = (int) bm.getResponseContentLength();

                if (jsessionid != null && bm.getResponseBodyAsString(len).indexOf(jsessionid) != -1) {
                    bm.releaseConnection();
                    return 0;
                }
                if (jsessionid == null && !checkcookie) {
                    return 0;
                }
                System.out.println("No cookies");
            }
            Header head = bm.getResponseHeader("getRequestedSessionId");
            if (head != null) {
                HeaderElement[] heade = head.getElements();
                requestedSessionId = heade[0].getValue();
            } else {
                requestedSessionId = null;
            }
        } else {
            System.out.println("response: " + httpResponseCode);
            System.out.println("response: " + bm.getStatusLine());
            response = bm.getResponseBodyAsString();
            System.out.println("response: " + response);
            success = false;
            httpClient = null;
        }
        // System.out.println("response:\n" + bm.getResponseBodyAsString(len)); 
    } catch (HttpException e) {
        e.printStackTrace();
        success = false;
        httpClient = null;
    }
    System.out.println("DONE: " + httpResponseCode);
    bm.releaseConnection();
    return httpResponseCode;
}

From source file:org.jboss.wise.core.client.jaxrs.impl.RSDynamicClientImpl.java

private void setRequestHeaders(HttpMethodBase method) {
    if (produceMediaTypes != null) {
        method.setRequestHeader("Content-Type", produceMediaTypes);
    }/*from w  w  w. j  a va  2 s  .co  m*/

    if (consumeMediaTypes != null) {
        method.setRequestHeader("Accept", consumeMediaTypes);
    }

    for (String headerName : requestHeaders.keySet()) {
        String headerValue = requestHeaders.get(headerName);

        method.setRequestHeader(headerName, headerValue);
    }
}

From source file:org.openlaszlo.data.HTTPDataSource.java

/**
 * @param since last modified time to use
 * @param req/*from   w w w.j av  a 2 s . c om*/
 * @param url if null, ignored
 * @param redirCount number of redirs we've done
 */
public static HttpData getDataOnce(HttpServletRequest req, HttpServletResponse res, long since, String surl,
        int redirCount, int timeout)
        throws IOException, HttpException, DataSourceException, MalformedURLException {

    HttpMethodBase request = null;
    HostConfiguration hcfg = new HostConfiguration();

    /*
      [todo hqm 2006-02-01] Anyone know why this code was here? It is setting
      the mime type to something which just confuses the DHTML parser.
              
      if (res != null) {
    res.setContentType("application/x-www-form-urlencoded;charset=UTF-8");
    }
    */

    try {

        // TODO: [2002-01-09 bloch] cope with cache-control
        // response headers (no-store, no-cache, must-revalidate, 
        // proxy-revalidate).

        if (surl == null) {
            surl = getURL(req);
        }
        if (surl == null || surl.equals("")) {
            throw new MalformedURLException(
                    /* (non-Javadoc)
                     * @i18n.test
                     * @org-mes="url is empty or null"
                     */
                    org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(),
                            "051018-312"));
        }

        String reqType = "";
        String headers = "";

        if (req != null) {
            reqType = req.getParameter("reqtype");
            headers = req.getParameter("headers");
        }

        boolean isPost = false;
        mLogger.debug("reqtype = " + reqType);

        if (reqType != null && reqType.equals("POST")) {
            request = new LZPostMethod();
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            isPost = true;
            mLogger.debug("setting POST req method");
        } else if (reqType != null && reqType.equals("PUT")) {
            request = new LZPutMethod();
            // todo [hqm 2007] treat PUT like POST? 
            isPost = true;
            mLogger.debug("setting PUT req method");
        } else if (reqType != null && reqType.equals("DELETE")) {
            request = new LZDeleteMethod();
            mLogger.debug("setting DELETE req method");
        } else {
            mLogger.debug("setting GET (default) req method");
            request = new LZGetMethod();
        }

        request.getParams().setVersion(mUseHttp11 ? HttpVersion.HTTP_1_1 : HttpVersion.HTTP_1_0);

        // Proxy the request headers
        if (req != null) {
            LZHttpUtils.proxyRequestHeaders(req, request);
        }

        // Set headers from query string
        if (headers != null && headers.length() > 0) {
            StringTokenizer st = new StringTokenizer(headers, "\n");
            while (st.hasMoreTokens()) {
                String h = st.nextToken();
                int i = h.indexOf(":");
                if (i > -1) {
                    String n = h.substring(0, i);
                    String v = h.substring(i + 2, h.length());
                    request.setRequestHeader(n, v);
                    mLogger.debug(
                            /* (non-Javadoc)
                             * @i18n.test
                             * @org-mes="setting header " + p[0] + "=" + p[1]
                             */
                            org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(),
                                    "051018-359", new Object[] { n, v }));
                }
            }
        }

        mLogger.debug("Parsing url");
        URI uri = LZHttpUtils.newURI(surl);
        try {
            hcfg.setHost(uri);
        } catch (Exception e) {
            throw new MalformedURLException(
                    /* (non-Javadoc)
                     * @i18n.test
                     * @org-mes="can't form uri from " + p[0]
                     */
                    org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-376",
                            new Object[] { surl }));
        }

        // This gets us the url-encoded (escaped) path and query string
        String path = uri.getEscapedPath();
        String query = uri.getEscapedQuery();
        mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="encoded path:  " + p[0]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-389",
                        new Object[] { path }));
        mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="encoded query: " + p[0]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-397",
                        new Object[] { query }));

        // This call takes a decoded (unescaped) path
        request.setPath(path);

        boolean hasQuery = (query != null && query.length() > 0);

        String rawcontent = null;
        // Newer rawpost protocol puts lzpostbody as a separate
        // top level query arg in the request.
        rawcontent = req.getParameter("lzpostbody");

        if (isPost) {
            // Older rawpost protocol put the "lzpostbody" arg
            // embedded in the "url" args's query args
            if (rawcontent == null && hasQuery) {
                rawcontent = findQueryArg("lzpostbody", query);
            }
            if (rawcontent != null) {
                // Get the unescaped query string
                ((EntityEnclosingMethod) request).setRequestEntity(new StringRequestEntity(rawcontent));
            } else if (hasQuery) {
                StringTokenizer st = new StringTokenizer(query, "&");
                while (st.hasMoreTokens()) {
                    String it = st.nextToken();
                    int i = it.indexOf("=");
                    if (i > 0) {
                        String n = it.substring(0, i);
                        String v = it.substring(i + 1, it.length());
                        // POST encodes values during request
                        ((PostMethod) request).addParameter(n, URLDecoder.decode(v, "UTF-8"));
                    } else {
                        mLogger.warn(
                                /* (non-Javadoc)
                                 * @i18n.test
                                 * @org-mes="ignoring bad token (missing '=' char) in query string: " + p[0]
                                 */
                                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(),
                                        "051018-429", new Object[] { it }));
                    }
                }
            }
        } else {
            // This call takes an encoded (escaped) query string
            request.setQueryString(query);
        }

        // Put in the If-Modified-Since headers
        if (since != -1) {
            String lms = LZHttpUtils.getDateString(since);
            request.setRequestHeader(LZHttpUtils.IF_MODIFIED_SINCE, lms);
            mLogger.debug(
                    /* (non-Javadoc)
                     * @i18n.test
                     * @org-mes="proxying lms: " + p[0]
                     */
                    org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-450",
                            new Object[] { lms }));
        }

        mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="setting up http client"
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-460"));
        HttpClient htc = null;
        if (mConnectionMgr != null) {
            htc = new HttpClient(mConnectionMgr);
        } else {
            htc = new HttpClient();
        }

        htc.setHostConfiguration(hcfg);

        // This is the data timeout
        mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="timeout set to " + p[0]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-478",
                        new Object[] { timeout }));
        htc.getParams().setSoTimeout(timeout);

        // Set connection timeout the same
        htc.getHttpConnectionManager().getParams().setConnectionTimeout(mConnectionTimeout);

        // Set timeout for getting a connection
        htc.getParams().setConnectionManagerTimeout(mConnectionPoolTimeout);

        // TODO: [2003-03-05 bloch] this should be more configurable (per app?)
        if (!isPost) {
            request.setFollowRedirects(mFollowRedirects > 0);
        }

        long t1 = System.currentTimeMillis();
        mLogger.debug("starting remote request");
        int rc = htc.executeMethod(hcfg, request);
        String status = HttpStatus.getStatusText(rc);
        if (status == null) {
            status = "" + rc;
        }
        mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="remote response status: " + p[0]
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-504",
                        new Object[] { status }));

        HttpData data = null;
        if (isRedirect(rc) && mFollowRedirects > redirCount) {
            String loc = request.getResponseHeader("Location").toString();
            String hostURI = loc.substring(loc.indexOf(": ") + 2, loc.length());
            mLogger.info(
                    /* (non-Javadoc)
                     * @i18n.test
                     * @org-mes="Following URL from redirect: " + p[0]
                     */
                    org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-517",
                            new Object[] { hostURI }));
            long t2 = System.currentTimeMillis();
            if (timeout > 0) {
                timeout -= (t2 - t1);
                if (timeout < 0) {
                    throw new InterruptedIOException(
                            /* (non-Javadoc)
                             * @i18n.test
                             * @org-mes=p[0] + " timed out after redirecting to " + p[1]
                             */
                            org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(),
                                    "051018-529", new Object[] { surl, loc }));
                }
            }

            data = getDataOnce(req, res, since, hostURI, redirCount++, timeout);
        } else {
            data = new HttpData(request, rc);
        }

        if (req != null && res != null) {
            // proxy response headers
            LZHttpUtils.proxyResponseHeaders(request, res, req.isSecure());
        }

        return data;

    } catch (ConnectTimeoutException ce) {
        // Transduce to an InterrupedIOException, since lps takes these to be timeouts.
        if (request != null) {
            request.releaseConnection();
        }
        throw new InterruptedIOException(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="connecting to " + p[0] + ":" + p[1] + " timed out beyond " + p[2] + " msecs."
                 */
                org.openlaszlo.i18n.LaszloMessages.getMessage(HTTPDataSource.class.getName(), "051018-557",
                        new Object[] { hcfg.getHost(), hcfg.getPort(), mConnectionTimeout }));
    } catch (HttpRecoverableException hre) {
        if (request != null) {
            request.releaseConnection();
        }
        throw hre;
    } catch (HttpException e) {
        if (request != null) {
            request.releaseConnection();
        }
        throw e;
    } catch (IOException ie) {
        if (request != null) {
            request.releaseConnection();
        }
        throw ie;
    } catch (RuntimeException e) {
        if (request != null) {
            request.releaseConnection();
        }
        throw e;
    }
}

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// ww  w  .  jav a2s  .  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) {
        }
    }
}

From source file:org.opentides.util.WidgetUtil.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/* w  w w. ja  va2 s .  c  o m*/
 * 
 * @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 = UrlUtil.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("html");
        return response;

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