Example usage for org.apache.http.client.methods HttpRequestBase setParams

List of usage examples for org.apache.http.client.methods HttpRequestBase setParams

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpRequestBase setParams.

Prototype

@Deprecated
    public void setParams(HttpParams httpParams) 

Source Link

Usage

From source file:org.geomajas.gwt2.example.base.server.mvc.AjaxProxyController.java

@RequestMapping(value = "/")
public final void proxyAjaxCall(@RequestParam(required = true, value = "url") String url,
        HttpServletRequest request, HttpServletResponse response) throws IOException {

    // URL needs to be url decoded
    url = URLDecoder.decode(url, "utf-8");

    HttpClient client = new DefaultHttpClient();
    try {/*from  w  ww .j a  v a2s . c o m*/
        HttpRequestBase proxyRequest;

        // Split this according to the type of request
        if ("GET".equals(request.getMethod())) {
            Enumeration<?> paramNames = request.getParameterNames();
            while (paramNames.hasMoreElements()) {
                String paramName = (String) paramNames.nextElement();
                if (!paramName.equalsIgnoreCase("url")) {
                    url = addParam(url, paramName, request.getParameter(paramName));
                }
            }

            proxyRequest = new HttpGet(url);
        } else if ("POST".equals(request.getMethod())) {
            proxyRequest = new HttpPost(url);

            // Set any eventual parameters that came with our original
            HttpParams params = new BasicHttpParams();
            Enumeration<?> paramNames = request.getParameterNames();
            while (paramNames.hasMoreElements()) {
                String paramName = (String) paramNames.nextElement();
                if (!"url".equalsIgnoreCase("url")) {
                    params.setParameter(paramName, request.getParameter(paramName));
                }
            }
            proxyRequest.setParams(params);
        } else {
            throw new NotImplementedException("This proxy only supports GET and POST methods.");
        }

        // Execute the method
        HttpResponse proxyResponse = client.execute(proxyRequest);

        // Set the content type, as it comes from the server
        Header[] headers = proxyResponse.getAllHeaders();
        for (Header header : headers) {
            if ("Content-Type".equalsIgnoreCase(header.getName())) {
                response.setContentType(header.getValue());
            }
        }

        // Write the body, flush and close
        proxyResponse.getEntity().writeTo(response.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:org.apache.axis2.transport.http.impl.httpclient4.HTTPSenderImpl.java

protected HttpResponse executeMethod(AbstractHttpClient httpClient, MessageContext msgContext, URL url,
        HttpRequestBase method) throws IOException {
    HttpHost httpHost = this.getHostConfiguration(httpClient, msgContext, url);

    // set the custom headers, if available
    addCustomHeaders(method, msgContext);

    // add compression headers if needed
    if (msgContext.isPropertyTrue(HTTPConstants.MC_ACCEPT_GZIP)) {
        method.addHeader(HTTPConstants.HEADER_ACCEPT_ENCODING, HTTPConstants.COMPRESSION_GZIP);
    }//from   ww  w.java 2s  .  c o m

    if (msgContext.isPropertyTrue(HTTPConstants.MC_GZIP_REQUEST)) {
        method.addHeader(HTTPConstants.HEADER_CONTENT_ENCODING, HTTPConstants.COMPRESSION_GZIP);
    }

    if (msgContext.getProperty(HTTPConstants.HTTP_METHOD_PARAMS) != null) {
        HttpParams params = (HttpParams) msgContext.getProperty(HTTPConstants.HTTP_METHOD_PARAMS);
        method.setParams(params);
    }

    String cookiePolicy = (String) msgContext.getProperty(HTTPConstants.COOKIE_POLICY);
    if (cookiePolicy != null) {
        method.getParams().setParameter(ClientPNames.COOKIE_POLICY, cookiePolicy);
    }

    setTimeouts(msgContext, method);
    HttpContext localContext = new BasicHttpContext();
    // Why do we have add context here
    return httpClient.execute(httpHost, method, localContext);
}

From source file:net.java.sip.communicator.service.httputil.HttpUtils.java

/**
 * Executes the method and return the result. Handle ask for password
 * when hitting password protected site.
 * Keep asking for password till user clicks cancel or enters correct
 * password. When 'remember password' is checked password is saved, if this
 * password and username are not correct clear them, if there are correct
 * they stay saved.//from w  ww .  j  av  a  2  s. c o m
 * @param httpClient the configured http client to use.
 * @param req the request for now it is get or post.
 * @param redirectHandler handles redirection, should we redirect and
 * the actual redirect.
 * @param parameters if we are redirecting we can use already filled
 * username and password in order to avoid asking the user twice.
 *
 * @return the result http entity.
 */
private static HttpEntity executeMethod(DefaultHttpClient httpClient, HttpRequestBase req,
        RedirectHandler redirectHandler, List<NameValuePair> parameters) throws Throwable {
    // do it when response (first execution) or till we are unauthorized
    HttpResponse response = null;
    int redirects = 0;
    while (response == null || response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED
            || response.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN) {
        // if we were unauthorized, lets clear the method and recreate it
        // for new connection with new credentials.
        if (response != null && (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED
                || response.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN)) {
            if (logger.isDebugEnabled())
                logger.debug("Will retry http connect and " + "credentials input as latest are not correct!");

            throw new AuthenticationException("Authorization needed");
        } else
            response = httpClient.execute(req);

        // if user click cancel no need to retry, stop trying
        if (!((HTTPCredentialsProvider) httpClient.getCredentialsProvider()).retry()) {
            if (logger.isDebugEnabled())
                logger.debug("User canceled credentials input.");
            break;
        }

        // check for post redirect as post redirects are not handled
        // automatically
        // RFC2616 (10.3 Redirection 3xx).
        // The second request (forwarded method) can only be a GET or HEAD.
        Header locationHeader = response.getFirstHeader("location");

        if (locationHeader != null && req instanceof HttpPost
                && (response.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY
                        || response.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY
                        || response.getStatusLine().getStatusCode() == HttpStatus.SC_SEE_OTHER)
                && redirects < MAX_REDIRECTS) {
            HttpRequestBase oldreq = req;
            oldreq.abort();

            String newLocation = locationHeader.getValue();

            // lets ask redirection handler if any
            if (redirectHandler != null && redirectHandler.handleRedirect(newLocation, parameters)) {
                return null;
            }

            req = new HttpGet(newLocation);
            req.setParams(oldreq.getParams());
            req.setHeaders(oldreq.getAllHeaders());

            redirects++;
            response = httpClient.execute(req);
        }
    }

    // if we finally managed to login return the result.
    if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        return response.getEntity();
    }

    // is user has canceled no result needed.
    return null;
}