Example usage for org.apache.http.impl.client CloseableHttpClient execute

List of usage examples for org.apache.http.impl.client CloseableHttpClient execute

Introduction

In this page you can find the example usage for org.apache.http.impl.client CloseableHttpClient execute.

Prototype

public <T> T execute(final HttpUriRequest request, final ResponseHandler<? extends T> responseHandler)
        throws IOException, ClientProtocolException 

Source Link

Document

Executes a request using the default context and processes the response using the given response handler.

Usage

From source file:ranktracker.crawler.google.SeoKeywordDetail.java

public static String fetchSemrushPage(String urlsrc) throws IOException {
    System.out.println("---------------Without Proxy-----------------");
    CloseableHttpClient httpclient = HttpClients.createDefault();
    String responseBody = "";
    try {/* w  w w  .j av  a2 s. com*/
        HttpGet httpget = new HttpGet(urlsrc);

        System.out.println("executing request " + httpget.getURI());

        // Create a custom response handler
        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
            public String handleResponse(final HttpResponse response)
                    throws ClientProtocolException, IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status <= 600) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }
        };
        responseBody = httpclient.execute(httpget, responseHandler);
        return responseBody;
    } catch (Exception e) {
        System.out.println("Exception in getting the sourec code from website :" + e);
    } finally {
        try {
            httpclient.close();
        } catch (Exception e) {
            System.out.println("Exception " + e);
        }
    }

    return responseBody;
}

From source file:org.dasein.cloud.utils.requester.AbstractDaseinRequestExecutor.java

protected T execute(HttpUriRequest httpUriRequest) throws DaseinRequestException {
    httpClientBuilder = setProxyIfRequired(httpClientBuilder);

    try {//from w  ww.ja v a2s . c  o m
        CloseableHttpClient httpClient = this.httpClientBuilder.build();
        try {
            return httpClient.execute(httpUriRequest, this.responseHandler);
        } finally {
            httpClient.close();
        }
    } catch (Exception e) {
        throw new DaseinRequestException(e.getMessage(), e);
    }
}

From source file:com.ibm.team.build.internal.hjplugin.util.HttpUtils.java

/**
 * Only used to test connection./* w w  w  .  ja  v a 2s .com*/
 * @param serverURI The RTC server
 * @param userId The userId to authenticate as
 * @param password The password to authenticate with
 * @param timeout The timeout period for the connection (in seconds)
 * @return The HttpContext to be used in a series of requests (so we only need to
 * login once).
 * @throws IOException Thrown if things go wrong
 * @throws InvalidCredentialsException if authentication fails
 * @throws GeneralSecurityException 
 */
public static void validateCredentials(String serverURI, String userId, String password, int timeout)
        throws IOException, GeneralSecurityException, InvalidCredentialsException {
    // We can't directly do a post because we need a JSession id cookie.
    // Instead attempt to do a get and then verify the credentials when we need to do
    // the form based auth. Don't bother to re-issue the get though. We just want
    // to know if the Login works

    CloseableHttpClient httpClient = getClient();
    HttpGet request = getGET(serverURI, timeout);
    HttpClientContext httpContext = createHttpContext();

    LOGGER.finer("GET: " + request.getURI()); //$NON-NLS-1$
    CloseableHttpResponse response = httpClient.execute(request, httpContext);
    try {
        response = authenticateIfRequired(response, httpClient, httpContext, serverURI, userId, password,
                timeout, null);
        if (response == null) {
            // retry get - if doing form based auth, not required
            // but if its basic auth then we do need to re-issue since basic just updates the context
            request = getGET(serverURI, timeout);
            response = httpClient.execute(request, httpContext);
            if (response.getStatusLine().getStatusCode() == 401) {
                // still not authorized
                throw new InvalidCredentialsException(
                        Messages.HttpUtils_authentication_failed(userId, serverURI));
            }
        }

    } finally {
        closeResponse(response);
    }
}

From source file:org.dasein.cloud.utils.requester.AbstractDaseinRequestExecutor.java

protected T execute(CloseableHttpClient httpClient, HttpUriRequest httpUriRequest)
        throws DaseinRequestException {
    try {//from w w w  . java  2 s.  co m
        return httpClient.execute(httpUriRequest, this.responseHandler);
    } catch (Exception e) {
        throw new DaseinRequestException(e.getMessage(), e);
    }
}