Example usage for org.apache.http.client HttpClient execute

List of usage examples for org.apache.http.client HttpClient execute

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:Main.java

public static String makeGETRequest(String url) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);

    // Fazendo a requisicao ao servidor
    try {// www. j  a v  a 2s.c om
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        return httpclient.execute(get, responseHandler);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    } catch (IllegalStateException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:org.lazydevs.api.gog.util.HttpClientUtil.java

public static HttpResponse get(HttpClient client, HttpContext context, String url) {

    HttpGet get = new HttpGet(url);
    HttpResponse response;//from  w  w  w.  j  av  a 2 s.  c o m

    try {
        response = client.execute(get, context);
    } catch (IOException e) {
        throw new GogApiException("Error executing GET request. url=" + url, e);
    }

    return response;
}

From source file:fr.liglab.adele.cilia.workbench.restmonitoring.utils.http.HttpHelper.java

public static String get(PlatformID platformID, String target) throws CiliaException {

    String url = getURL(platformID, target);
    HttpUriRequest httpRequest = new HttpGet(url);
    HttpClient httpClient = getClient();
    ResponseHandler<String> responseHandler = new BasicResponseHandler();

    try {//w w w .  j a v  a 2s.com
        String retval = httpClient.execute(httpRequest, responseHandler);
        httpClient.getConnectionManager().shutdown();
        return retval;
    } catch (ConnectTimeoutException e) {
        httpClient.getConnectionManager().shutdown();
        throw new CiliaException("Can't joint " + url + " before timeout (" + timeout + "ms)");
    } catch (ClientProtocolException e) {
        httpClient.getConnectionManager().shutdown();
        throw new CiliaException("Can't joint " + url, e);
    } catch (IOException e) {
        httpClient.getConnectionManager().shutdown();
        throw new CiliaException("Can't joint " + url, e);
    }
}

From source file:com.github.caldav4j.support.HttpClientTestUtils.java

public static <R, M extends HttpRequestBase, E extends Exception> R executeMethod(int expectedStatus, M method,
        HttpClient httpClient, HttpHost httpHost, HttpMethodCallback<R, M, E> methodCallback)
        throws IOException, E {
    try {/*from w  w w. j  av a  2  s.com*/
        HttpResponse response = httpClient.execute(httpHost, method);
        int actualStatus = response.getStatusLine().getStatusCode();
        assertEquals("Response status", expectedStatus, actualStatus);

        return (methodCallback != null) ? methodCallback.getResponse(method, response) : null;
    } finally {
        method.reset();
    }
}

From source file:com.github.horrorho.inflatabledonkey.cloud.accounts.Accounts.java

public static Account account(HttpClient httpClient, Auth auth) throws IOException {
    HttpUriRequest accountSettingsRequest = AccountSettingsRequestFactory.instance().apply(auth.dsPrsID(),
            auth.mmeAuthToken());/*from  w  w w.  j a v  a 2 s  .  co m*/

    NSDictionary settings = httpClient.execute(accountSettingsRequest,
            PropertyListResponseHandler.nsDictionaryResponseHandler());

    return account(settings);
}

From source file:com.github.horrorho.inflatabledonkey.cloud.escrow.EscrowOperationsRecords.java

public static NSDictionary records(HttpClient httpClient, EscrowProxyRequestFactory requests)
        throws IOException {
    /* //from  w w  w. j ava 2  s.  c  om
    EscrowService SRP-6a exchanges: GETRECORDS
     */
    HttpUriRequest recordsRequest = requests.getRecords();
    NSDictionary dictionary = httpClient.execute(recordsRequest, RESPONSE_HANDLER);
    logger.debug("-- records() - GETRECORDS: {}", dictionary.toXMLPropertyList());
    return dictionary;
}

From source file:com.devdungeon.simplejson.DecodeJson.java

public static String getJsonFromUrl(String url) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    httpget.setHeader("User-Agent", "Java Reddit Test Client 0.0.1");
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = null;//from   ww  w  .  j  a  va2  s.co m
    try {
        responseBody = httpclient.execute(httpget, responseHandler);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    httpclient.getConnectionManager().shutdown();
    return responseBody;
}

From source file:com.aquatest.webservice.AquaTestWebService.java

/**
 * Sends an HTTP GET request to the url.
 * //from   w w  w.j a  v a2  s  . co m
 * @param url
 *            URL to send GET request to
 * @return HttpResponse object
 * @throws IOException
 *             if there is an IO error during the request
 * @throws ClientProtocolException
 *             if there is a protocol error during the request
 */
// method declared static for Android optimisation
public static HttpResponse sendGetRequest(String url) throws ClientProtocolException, IOException {
    // create http GET request object
    // Log.v("WEBSERVICE", "creating GET request to [" + url + "]");
    HttpGet get = new HttpGet(url);

    // send GET message to the url
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpResponse response = httpClient.execute(get, localContext);

    return response;
}

From source file:org.epop.dataprovider.googlescholar.GoogleScholarGetterFromId.java

static List<Literature> getFromId(String userId) {
    // http://scholar.google.it/citations?user=q21xxm4AAAAJ&pagesize=100
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("user", userId));
    qparams.add(new BasicNameValuePair("pagesize", "100"));

    URI uri;//from www  .j a  va  2s . co  m
    String responseBody = null;
    try {
        uri = URIUtils.createURI("http", GOOGLE_SCHOLAR, -1, "", URLEncodedUtils.format(qparams, "UTF-8"),
                null);
        uri = new URI(uri.toString().replace("citations/?", "citations?"));
        HttpGet httpget = new HttpGet(uri);
        System.out.println(httpget.getURI());
        HttpClient httpclient = new DefaultHttpClient();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        responseBody = httpclient.execute(httpget, responseHandler);
        //System.out.println(responseBody);
        int counter = 1;
        String newResponseBody = responseBody;
        while (newResponseBody.contains("class=\"cit-dark-link\">Next &gt;</a>")) {
            URI newUri = new URI(uri.toString() + "&cstart=" + counter * 100);
            httpget = new HttpGet(newUri);
            System.out.println(httpget.getURI());
            httpclient = new DefaultHttpClient();
            newResponseBody = httpclient.execute(httpget, responseHandler);
            //System.out.println(newResponseBody);
            responseBody = responseBody + newResponseBody;
            counter++;
        }
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // return the result as string
    return parsePage(new StringReader(responseBody));
}

From source file:oracle.custom.ui.oauth.vo.OpenIdConfiguration.java

public static OpenIdConfiguration getButDontSave(String baseUrl) throws Exception {

    String url = baseUrl + endpoint;
    System.out.println("URL is '" + url + "'");
    HttpClient client = ServerUtils.getClient();
    URI uri = new URI(url);
    HttpGet get = new HttpGet(uri);
    HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    HttpResponse response = client.execute(host, get);
    try {//from w  w  w. j  av  a 2 s.  com
        HttpEntity entity2 = response.getEntity();
        String res = EntityUtils.toString(entity2);
        EntityUtils.consume(entity2);
        ObjectMapper mapper = new ObjectMapper();
        //res = res.replaceAll("secureoracle.idcs.internal.oracle.com:443", "oc-140-86-12-131.compute.oraclecloud.com");
        OpenIdConfiguration openIdConfigInstance = mapper.readValue(res, OpenIdConfiguration.class);

        return openIdConfigInstance;
    } finally {
        if (response instanceof CloseableHttpResponse) {
            ((CloseableHttpResponse) response).close();
        }
    }
}