Example usage for org.apache.http.client.methods HttpHead HttpHead

List of usage examples for org.apache.http.client.methods HttpHead HttpHead

Introduction

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

Prototype

public HttpHead(final String uri) 

Source Link

Usage

From source file:com.messagemedia.restapi.client.v1.internal.RestRequest.java

/**
 * Builds an apache http request/*  w ww  . jav a 2 s  .c om*/
 *
 * @return the apache http request
 */
HttpUriRequest getHttpRequest() {
    HttpUriRequest request;
    switch (method) {
    case GET:
        request = new HttpGet(url);
        break;
    case DELETE:
        request = new HttpDelete(url);
        break;
    case HEAD:
        request = new HttpHead(url);
        break;
    case POST:
        HttpPost post = new HttpPost(url);
        post.setEntity(new ByteArrayEntity(body));
        request = post;
        break;
    case PUT:
        HttpPut put = new HttpPut(url);
        put.setEntity(new ByteArrayEntity(body));
        request = put;
        break;
    case PATCH:
        HttpPatch patch = new HttpPatch(url);
        patch.setEntity(new ByteArrayEntity(body));
        request = patch;
        break;
    default:
        throw new RuntimeException("Method not supported");
    }

    addHeaders(request);

    return request;
}

From source file:biz.varkon.shelvesom.util.CookieStore.java

public String getCookie(String url) {
    final CookieManager cookieManager = CookieManager.getInstance();
    String cookie = cookieManager.getCookie(url);

    if (cookie == null || cookie.length() == 0) {
        final HttpHead head = new HttpHead(url);
        HttpEntity entity = null;// w w  w  .  ja va  2 s  .  c  o  m
        try {
            final HttpResponse response = HttpManager.execute(head);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                entity = response.getEntity();

                final Header[] cookies = response.getHeaders("set-cookie");
                for (Header cooky : cookies) {
                    cookieManager.setCookie(url, cooky.getValue());
                }

                CookieSyncManager.getInstance().sync();
                cookie = cookieManager.getCookie(url);
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Could not retrieve cookie", e);
        } finally {
            if (entity != null) {
                try {
                    entity.consumeContent();
                } catch (IOException e) {
                    Log.e(LOG_TAG, "Could not retrieve cookie", e);
                }
            }
        }
    }

    return cookie;
}

From source file:com.yaauie.unfurl.UrlExpander.java

public URI expand(URI uri) throws IOException {
    final HttpHead request = new HttpHead(uri);
    final HttpClientContext context = new HttpClientContext();

    httpClient.execute(request, context);

    final List<URI> redirectLocations = context.getRedirectLocations();
    if (redirectLocations == null || redirectLocations.isEmpty()) {
        return uri;
    } else {/* www  .jav a  2 s.  co m*/
        return redirectLocations.get(redirectLocations.size() - 1);
    }
}

From source file:org.dataconservancy.dcs.ingest.client.impl.HttpDepositDocument.java

public long getLastModified() {
    poll(new HttpHead(docUrl));

    for (Header h : headers) {
        if (h.getName().equals(HttpHeaderUtil.LAST_MODIFIED)) {
            return DateUtility.parseDate(h.getValue());
        }/*from w  w w .  j a  va 2s .c o m*/
    }

    logger.warn("Could not get last modified date from server.  " + "Using current date");
    return new Date().getTime();

}

From source file:com.google.android.apps.authenticator.timesync.NetworkTimeProvider.java

/**
 * Gets the system time by issuing a request over the network.
 *
 * @return time (milliseconds since epoch).
 *
 * @throws IOException if an I/O error occurs.
 *//*from w w w.  j av  a 2  s .  c o m*/
public long getNetworkTime() throws IOException {
    HttpHead request = new HttpHead(URL);
    Log.i(LOG_TAG, "Sending request to " + request.getURI());
    HttpResponse httpResponse;
    try {
        httpResponse = mHttpClient.execute(request);
    } catch (ClientProtocolException e) {
        throw new IOException(String.valueOf(e));
    } catch (IOException e) {
        throw new IOException("Failed due to connectivity issues: " + e);
    }

    try {
        Header dateHeader = httpResponse.getLastHeader("Date");
        Log.i(LOG_TAG, "Received response with Date header: " + dateHeader);
        if (dateHeader == null) {
            throw new IOException("No Date header in response");
        }
        String dateHeaderValue = dateHeader.getValue();
        try {
            Date networkDate = DateUtils.parseDate(dateHeaderValue);
            return networkDate.getTime();
        } catch (DateParseException e) {
            throw new IOException("Invalid Date header format in response: \"" + dateHeaderValue + "\"");
        }
    } finally {
        // Consume all of the content of the response to facilitate HTTP 1.1 persistent connection
        // reuse and to avoid running out of connections when this methods is scheduled on different
        // threads.
        try {
            HttpEntity responseEntity = httpResponse.getEntity();
            if (responseEntity != null) {
                responseEntity.consumeContent();
            }
        } catch (IOException e) {
            // Ignored because this is not an error that is relevant to clients of this transport.
        }
    }
}

From source file:hmock.BasicHMockTest.java

@Test
public void testParamHeadRequest() throws Exception {

    HMock.respond().status(201).when().head("/employees").param("name", equalTo("John"));

    HttpHead head = new HttpHead("http://localhost:7357/employees?name=John");

    HttpResponse response = _httpclient.execute(head);

    assertEquals(201, response.getStatusLine().getStatusCode());

    head = new HttpHead("http://localhost:7357/employees?name=John2");
    response = _httpclient.execute(head);

    assertEquals(404, response.getStatusLine().getStatusCode());
}

From source file:com.couchbase.capi.TestCAPI.java

public void testDatabaseHeadDoesNotExist() throws Exception {
    HttpClient client = getClient();/*from   ww  w  .j a v  a  2 s  .c o  m*/

    HttpUriRequest request = new HttpHead(String.format("http://localhost:%d/doesnotexist", port));
    HttpResponse response = client.execute(request);

    Assert.assertEquals(404, response.getStatusLine().getStatusCode());
}

From source file:com.payu.ratel.tests.HealthcheckTest.java

@Test
public void shouldReportServiceAviability() throws Exception {

    //when//from  w  w w. j a v  a2s.  com
    String serviceAddress = strategiesProvider.getFetchStrategy()
            .fetchServiceAddress(TestService.class.getCanonicalName());

    HttpClient client = new DefaultHttpClient();
    HttpUriRequest request = new HttpHead(serviceAddress);

    HttpResponse response = client.execute(request);

    //then

    assertThat(response.getStatusLine().getStatusCode()).isEqualTo(HttpServletResponse.SC_OK);
}

From source file:org.dalmasso.ietfsched.io.RemoteExecutor.java

public String executeHead(String url) throws Exception {
    final HttpUriRequest request = new HttpHead(url);
    final HttpResponse resp = mHttpClient.execute(request);
    final int status = resp.getStatusLine().getStatusCode();
    if (status != HttpStatus.SC_OK) {
        Log.d(TAG, "Response Code " + status);
        throw new IOException(
                "Unexpected server response " + resp.getStatusLine() + " for " + request.getRequestLine());
    }//ww  w .ja va  2  s. c  o m
    //      Header h = resp.getFirstHeader("Content-Length");
    Header h = resp.getFirstHeader("Etag");
    if (h != null) {
        try {
            //            int length = Integer.parseInt(h.getValue());
            //            Log.d(TAG, "Content-Length " + length);
            String etag = h.getValue();
            Log.d(TAG, "Etag " + h.getValue());
            return etag;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    return null;
}