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

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

Introduction

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

Prototype

public HttpGet(final String uri) 

Source Link

Usage

From source file:com.android.dialer.lookup.LookupUtils.java

public static String httpGet(HttpGet request) throws IOException {
    HttpClient client = new DefaultHttpClient();

    request.setHeader("User-Agent", USER_AGENT);

    HttpResponse response = client.execute(request);
    int status = response.getStatusLine().getStatusCode();

    // Android's org.apache.http doesn't have the RedirectStrategy class
    if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY) {
        Header[] headers = response.getHeaders("Location");

        if (headers != null && headers.length != 0) {
            HttpGet newGet = new HttpGet(headers[headers.length - 1].getValue());
            for (Header header : request.getAllHeaders()) {
                newGet.addHeader(header);
            }/*w w w .ja  v a2s  . c om*/
            return httpGet(newGet);
        } else {
            throw new IOException("Empty redirection header");
        }
    }

    if (status != HttpStatus.SC_OK) {
        throw new IOException("HTTP failure (status " + status + ")");
    }

    return EntityUtils.toString(response.getEntity());
}

From source file:net.bither.http.HttpGetResponse.java

public void handleHttpGet() throws Exception {
    setHttpClient();/*from www.  j a va2 s  .com*/
    try {
        HttpGet httpGet = new HttpGet(getUrl());
        httpGet.setHeader("Accept", "application/json");

        HttpResponse httpResponse = getHttpClient().execute(httpGet);
        String response = getReponse(httpResponse);
        setResult(response);
    } catch (Exception e) {
        throw e;
    } finally {
        getHttpClient().getConnectionManager().shutdown();
    }
}

From source file:net.bither.image.http.HttpGetResponse.java

public void handleHttpGet() throws Exception {
    setHttpClient();/*from ww w.j a  va  2 s .  com*/
    try {
        HttpGet httpGet = new HttpGet(getUrl());
        httpGet.setHeader("Accept", "application/json");
        HttpResponse httpResponse = getHttpClient().execute(httpGet);
        String response = getReponse(httpResponse);
        setResult(response);
    } catch (Exception e) {
        throw e;
    } finally {

        getHttpClient().getConnectionManager().shutdown();
    }
}

From source file:org.androidappdev.codebits.CodebitsApi.java

/**
 * Perform an HTTP request/*w  ww.  j a  va  2s  .c  o  m*/
 * 
 * @param url
 *            the URL for the request
 * @return a String with the result of the request
 */
private static String performHttpRequest(String url) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    try {
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(content));
        String line = null;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
    } catch (Exception e) {
        Log.d(TAG, "Exception", e);
    }
    return builder.toString();
}

From source file:io.github.thred.climatetray.ClimateTrayUtils.java

public static BuildInfo performBuildInfoRequest() {
    try {//from  w w  w. java 2  s.com
        CloseableHttpClient client = ClimateTray.PREFERENCES.getProxySettings().createHttpClient();
        HttpGet request = new HttpGet(BUILD_INFO_URL);
        CloseableHttpResponse response;

        try {
            response = client.execute(request);
        } catch (IOException e) {
            ClimateTray.LOG.warn("Failed to request build information from \"%s\".", e, BUILD_INFO_URL);

            consumeUpdateFailed();

            return null;
        }

        try {
            int status = response.getStatusLine().getStatusCode();

            if ((status >= 200) && (status < 300)) {
                try (InputStream in = response.getEntity().getContent()) {
                    BuildInfo buildInfo = BuildInfo.create(in);

                    ClimateTray.LOG.info("Build Information: %s", buildInfo);

                    return buildInfo;
                }
            } else {
                ClimateTray.LOG.warn("Request to \"%s\" failed with error %d.", BUILD_INFO_URL, status);

                consumeUpdateFailed();
            }
        } finally {
            response.close();
        }
    } catch (Exception e) {
        ClimateTray.LOG.warn("Failed to request build information.", e);
    }

    return null;
}

From source file:org.commonjava.maven.firth.client.ArtifactClient.java

public static ArtifactClient load(final String baseUrl, final String mapsBasePath,
        final String packagePathFormat, final String source, final String version) throws IOException {
    final HttpClient http = HttpClientBuilder.create().build();

    // Assume something like: http://download.devel.redhat.com/brewroot/artifact-maps/rcm-mw-tools-build/1234/composition.json
    final String compsUrl = PathUtils.normalize(baseUrl, mapsBasePath, source, version, "composition.json");

    final ObjectMapper om = new ObjectMapper();

    HttpGet request = new HttpGet(compsUrl);
    HttpResponse response = http.execute(request);

    SourceComposition comp;/*from   ww w .j  a va 2 s  .c  om*/
    if (response.getStatusLine().getStatusCode() == 200) {
        comp = om.readValue(response.getEntity().getContent(), SourceComposition.class);
    } else {
        throw new IOException("Failed to read composition from: " + compsUrl);
    }

    final List<String> manifestSources = new ArrayList<String>();
    manifestSources.add(source);
    manifestSources.addAll(comp.getComposedOf());

    final List<SourceManifest> manifests = new ArrayList<SourceManifest>();
    for (final String src : manifestSources) {
        // Assume something like: http://download.devel.redhat.com/brewroot/artifact-maps/rcm-mw-tools-build/1234/manifest.json
        final String manifestUrl = PathUtils.normalize(baseUrl, mapsBasePath, src, version, "manifest.json");
        request = new HttpGet(manifestUrl);
        response = http.execute(request);
        if (response.getStatusLine().getStatusCode() == 200) {
            final SourceManifest manifest = om.readValue(response.getEntity().getContent(),
                    SourceManifest.class);

            manifests.add(manifest);
        } else {
            throw new IOException("Failed to read manifest from: " + manifestUrl);
        }
    }

    final ArtifactMap am = new ArtifactMap(packagePathFormat, manifests);

    return new ArtifactClient(baseUrl, am, http);
}

From source file:functional.PathTraversingTest.java

@Test
public void testNoTraversal() throws Exception {
    HttpGet httpget = new HttpGet("http://localhost:8080/");

    CloseableHttpResponse execute = httpclient.execute(httpget);
    execute.close();//from  www  . j a va2 s .  com
    assertEquals(200, execute.getStatusLine().getStatusCode());
}

From source file:com.oneapm.base.tools.UrlPostMethod.java

public static String urlGetMethod(String url) {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet get = new HttpGet(url);
    HttpEntity entity = null;/*from   ww w. j  av  a2  s . c  om*/
    String json = null;
    try {
        CloseableHttpResponse response = httpClient.execute(get);
        json = EntityUtils.toString(response.getEntity(), "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
    }
    return json.toString();

}

From source file:org.n52.shetland.util.HTTP.java

public static String getAsString(URI uri) throws IOException {
    try (CloseableHttpResponse response = CLIENT.execute(new HttpGet(uri))) {
        HttpEntity entity = response.getEntity();
        String encoding = Optional.ofNullable(entity.getContentEncoding()).map(Header::getValue)
                .orElse(StandardCharsets.UTF_8.name());
        Charset charset = Charset.forName(encoding);
        try (InputStream is = entity.getContent(); Reader reader = new InputStreamReader(is, charset)) {
            return CharStreams.toString(reader);
        }//from  w  w  w. ja v  a  2  s. c o  m
    }
}

From source file:biz.webgate.tools.urlfetcher.URLFetcher.java

public static PageAnalyse analyseURL(String url, int maxPictureHeight) throws FetcherException {
    PageAnalyse analyse = new PageAnalyse(url);
    HttpClient httpClient = null;//from  ww  w  .j  a v  a  2 s  .c o  m
    try {
        httpClient = new DefaultHttpClient();
        ((DefaultHttpClient) httpClient).setRedirectStrategy(new DefaultRedirectStrategy());
        HttpGet httpGet = new HttpGet(url);
        httpGet.addHeader("Content-Type", "text/html; charset=utf-8");
        HttpResponse response = httpClient.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode == STATUS_OK) {
            HttpEntity entity = response.getEntity();
            parseContent(maxPictureHeight, analyse, entity.getContent());

        } else {
            throw new FetcherException("Response from WebSite is :" + statusCode);
        }
    } catch (IllegalStateException e) {
        throw new FetcherException(e);
    } catch (FetcherException e) {
        throw e;
    } catch (Exception e) {
        throw new FetcherException(e);
    } finally {
        if (httpClient != null) {
            httpClient.getConnectionManager().shutdown();
        }
    }
    return analyse;
}