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:com.griddynamics.jagger.invoker.http.ApacheAbstractHttpInvoker.java

@Override
public final HttpResponse invoke(Q query, String endpoint) throws InvocationException {
    Preconditions.checkNotNull(query);//from  w w  w. j  a  v  a2  s  .c  o  m
    Preconditions.checkNotNull(endpoint);

    HttpRequestBase method = null;
    HttpEntity response = null;
    try {
        method = getHttpMethod(query, endpoint);
        method.setParams(getHttpClientParams(query));

        org.apache.http.HttpResponse httpResponse = httpClient.execute(method);
        response = httpResponse.getEntity();
        return HttpResponse.create(httpResponse.getStatusLine().getStatusCode(),
                EntityUtils.toString(response));
    } catch (Exception e) {
        if (method != null) {
            log.debug("Error during invocation with URL: " + method.getURI() + ", endpoint: " + endpoint
                    + ", query: " + query, e);
        } else {
            log.debug("Error during invocation with: endpoint: " + endpoint + ", query: " + query, e);
        }
        throw new InvocationException("InvocationException : ", e);
    } finally {
        EntityUtils.consumeQuietly(response);
    }
}

From source file:com.predic8.membrane.examples.tests.integration.OAuth2RaceCondition.java

private void setNoRedirects(HttpRequestBase get) {
    BasicHttpParams params = new BasicHttpParams();
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
    get.setParams(params);
}

From source file:com.sap.core.odata.fit.basic.UrlRewriteTest.java

private HttpRequestBase createRedirectRequest(final Class<? extends HttpRequestBase> clazz) throws Exception {
    String endpoint = getEndpoint().toASCIIString();
    endpoint = endpoint.substring(0, endpoint.length() - 1);

    final HttpRequestBase httpMethod = clazz.newInstance();
    httpMethod.setURI(URI.create(endpoint));

    final HttpParams params = new BasicHttpParams();
    params.setParameter("http.protocol.handle-redirects", false);
    httpMethod.setParams(params);
    return httpMethod;
}

From source file:eu.prestoprime.p4gui.connection.P4HttpClient.java

public HttpResponse executeRequest(HttpRequestBase request) throws IOException {
    // set userID
    request.setHeader(new BasicHeader("userID", this.userID));

    // disable redirect handling
    HttpParams params = new BasicHttpParams();
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
    request.setParams(params);

    // execute request
    HttpResponse response = super.execute(request);

    // check redirect
    if (redirectCodes.contains(response.getStatusLine().getStatusCode())) {
        logger.debug("Redirecting...");

        // get newURL
        String newURL = response.getFirstHeader("Location").getValue();

        // create newRequest
        try {// www  .j a v  a  2s .  c o m
            HttpUriRequest newRequest = request.getClass().getDeclaredConstructor(String.class)
                    .newInstance(newURL);

            // copy entity
            if (request instanceof HttpEntityEnclosingRequestBase) {
                HttpEntity entity = ((HttpEntityEnclosingRequestBase) request).getEntity();
                if (entity != null) {
                    logger.debug("Cloning entity...");

                    ((HttpEntityEnclosingRequestBase) newRequest).setEntity(entity);
                }
            }

            // set userID
            newRequest.setHeader(new BasicHeader("userID", this.userID));

            // retry
            response = new P4HttpClient(userID).execute(newRequest);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException
                | InstantiationException e) {
            e.printStackTrace();
        }
    }

    return response;
}

From source file:com.dhenton9000.filedownloader.FileDownloader.java

private HttpResponse getHTTPResponse() throws IOException, NullPointerException {
    if (fileURI == null)
        throw new NullPointerException("No file URI specified");

    HttpClient client = new DefaultHttpClient();
    BasicHttpContext localContext = new BasicHttpContext();

    //Clear down the local cookie store every time to make sure we don't have any left over cookies influencing the test
    localContext.setAttribute(ClientContext.COOKIE_STORE, null);

    LOG.info("Mimic WebDriver cookie state: " + mimicWebDriverCookieState);
    if (mimicWebDriverCookieState) {
        localContext.setAttribute(ClientContext.COOKIE_STORE, mimicCookieState(driver.manage().getCookies()));
    }/*from   ww  w  .  j av  a 2s .  c  om*/

    HttpRequestBase requestMethod = httpRequestMethod.getRequestMethod();
    requestMethod.setURI(fileURI);
    HttpParams httpRequestParameters = requestMethod.getParams();
    httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects);
    requestMethod.setParams(httpRequestParameters);
    //TODO if post send map of variables, also need to add a post map setter

    LOG.info("Sending " + httpRequestMethod.toString() + " request for: " + fileURI);
    return client.execute(requestMethod, localContext);
}

From source file:com.graphaware.test.util.TestHttpClient.java

protected void setParams(HttpRequestBase method, Map<String, String> params) {
    if (params == null) {
        return;/*from   w w  w. jav a2 s  .  c om*/
    }

    HttpParams httpParams = new BasicHttpParams();
    for (Map.Entry<String, String> paramEntry : params.entrySet()) {
        httpParams.setParameter(paramEntry.getKey(), paramEntry.getValue());
    }
    method.setParams(httpParams);
}

From source file:com.mikecorrigan.bohrium.pubsub.Transaction.java

private int read(HttpRequestBase request) {
    Log.v(TAG, "read");

    CookieStore mCookieStore = new BasicCookieStore();
    mCookieStore.addCookie(mAuthCookie);

    DefaultHttpClient httpClient = new DefaultHttpClient();
    BasicHttpContext mHttpContext = new BasicHttpContext();
    mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);

    try {//  w  w w.j av a 2s .  co m
        final HttpParams getParams = new BasicHttpParams();
        HttpClientParams.setRedirecting(getParams, false);
        request.setParams(getParams);

        request.setHeader("Accept", getMimeType());

        HttpResponse response = httpClient.execute(request, mHttpContext);
        Log.d(TAG, "status=" + response.getStatusLine());

        // Read response body.
        HttpEntity responseEntity = response.getEntity();
        if (responseEntity != null) {
            InputStream is = responseEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
            is.close();

            mStatusCode = response.getStatusLine().getStatusCode();
            mStatusReason = response.getStatusLine().getReasonPhrase();
            if (mStatusCode == 200) {
                mResponseBody = decode(sb.toString());
                Log.v(TAG, "mResponseBody=" + sb.toString());
            }
            return mStatusCode;
        }
    } catch (IOException e) {
        Log.e(TAG, "exception=" + e);
        Log.e(TAG, Log.getStackTraceString(e));
    } finally {
        httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
    }

    return mStatusCode;
}

From source file:org.hyperic.hq.plugin.netservices.HTTPCollector.java

private void addParams(HttpRequestBase request, Map<String, String> params)
        throws UnsupportedEncodingException {
    if (params != null && !params.isEmpty()) {
        BasicHttpParams prms = new BasicHttpParams();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            prms.setParameter(entry.getKey(), entry.getValue());
        }//from  ww  w .  j  a  v  a  2  s  .c o m
        request.setParams(prms);
    }
}

From source file:gr.ndre.scuttloid.APITask.java

protected HttpRequestBase buildRequest() {
    HttpRequestBase request;
    if (this.method == METHOD_POST) {
        request = new HttpPost(this.url);
        if (this.data != null) {
            try {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(this.data, HTTP.UTF_8);
                ((HttpEntityEnclosingRequestBase) request).setEntity(entity);
            } catch (UnsupportedEncodingException e) {
                this.status = GENERIC_ERROR;
                return null;
            }//from  www . j a va 2  s.c o  m
        }
    } else {
        request = new HttpGet(this.url);
    }

    // Set timeout limits
    BasicHttpParams http_parameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(http_parameters, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(http_parameters, SOCKET_TIMEOUT);
    request.setParams(http_parameters);

    // Add Basic Authentication header
    this.addAuthHeader(request);

    return request;
}

From source file:pl.psnc.synat.wrdz.zmkd.invocation.RestServiceCaller.java

/**
 * Invoke a service according to the execution info.
 * //w  w  w  .j  a v  a2 s .c  o  m
 * In case of success, the entity stream is not closed.
 * 
 * @param execInfo
 *            info how to execute a REST service
 * @return execution outcome
 * @throws InvalidHttpRequestException
 *             when constructed request is invalid
 * @throws InvalidHttpResponseException
 *             when response is invalid
 */
public ExecutionOutcome invoke(ExecutionInfo execInfo)
        throws InvalidHttpRequestException, InvalidHttpResponseException {
    HttpClient httpClient = getHttpClient();
    HttpRequestBase request = null;
    try {
        request = RestServiceCallerUtils.constructServiceRequestBase(execInfo);
    } catch (URISyntaxException e) {
        logger.error("Incorrect URI of the service.", e);
        throw new InvalidHttpRequestException(e);
    }
    if (request instanceof HttpEntityEnclosingRequestBase) {
        HttpEntity entity = RestServiceCallerUtils.constructServiceRequestEntity(execInfo);
        if (entity != null) {
            ((HttpEntityEnclosingRequestBase) request).setEntity(entity);
        }
        Header header = RestServiceCallerUtils.constructServiceRequestHeader(execInfo);
        if (header != null) {
            request.setHeader(header);
        }
    }

    HttpParams params = new BasicHttpParams();
    params.setParameter("http.protocol.handle-redirects", false);
    request.setParams(params);

    HttpResponse response = null;
    try {
        response = httpClient.execute(request);
    } catch (ClientProtocolException e) {
        logger.error("Incorrect protocol.", e);
        throw new InvalidHttpResponseException(e);
    } catch (IOException e) {
        logger.error("IO error during execution.", e);
        throw new InvalidHttpResponseException(e);
    }
    try {
        return RestServiceCallerUtils.retrieveOutcome(response);
    } catch (IllegalStateException e) {
        logger.error("Cannot retrived the stream with the content.", e);
        EntityUtils.consumeQuietly(response.getEntity());
        throw new InvalidHttpResponseException(e);
    } catch (IOException e) {
        logger.error("IO error when retrieving content.", e);
        EntityUtils.consumeQuietly(response.getEntity());
        throw new InvalidHttpResponseException(e);
    }
    // remember to close the entity stream after read it.  
}